Skip to content

Commit

Permalink
New option: enable XSendFile to improve downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
ignacionelson committed Jul 6, 2020
1 parent 8ee2ec4 commit 6f90112
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 38 deletions.
49 changes: 28 additions & 21 deletions download.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,27 +89,34 @@
$real_file = UPLOADED_FILES_DIR.DS.basename($real_file_url);
$random_file = realpath(UPLOADED_FILES_DIR.DS.basename($file_on_disk));
if (file_exists($random_file)) {
session_write_close();
while (ob_get_level()) ob_end_clean();
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($real_file));
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Cache-Control: private',false);
header('Content-Length: ' . get_real_size($random_file));
header('Connection: close');
//readfile($real_file);

$context = stream_context_create();
$file = fopen($random_file, 'rb', FALSE, $context);
while ( !feof( $file ) ) {
//usleep(1000000); //Reduce download speed
echo stream_get_contents($file, 2014);
}

fclose( $file );
die();
if (defined('XSENDFILE_ENABLE') && XSENDFILE_ENABLE == 1) {
header("X-Sendfile: $random_file");
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($real_file));
exit;
} else {
session_write_close();
while (ob_get_level()) ob_end_clean();
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($real_file));
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Cache-Control: private',false);
header('Content-Length: ' . get_real_size($random_file));
header('Connection: close');
//readfile($real_file);

$context = stream_context_create();
$file = fopen($random_file, 'rb', FALSE, $context);
while ( !feof( $file ) ) {
//usleep(1000000); //Reduce download speed
echo stream_get_contents($file, 2014);
}

fclose( $file );
exit;
}
}
}
}
Expand Down
43 changes: 26 additions & 17 deletions includes/Classes/DoProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,24 +350,33 @@ private function serveFile($filename, $save_as)
if (file_exists($filename)) {
session_write_close();
while (ob_get_level()) ob_end_clean();
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($save_as));
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Cache-Control: private',false);
header('Content-Length: ' . get_real_size($filename));
header('Connection: close');
//readfile($this->file_location);

$this->context = stream_context_create();
$this->file = fopen($filename, 'rb', false, $this->context);
while ( !feof( $this->file ) ) {
//usleep(1000000); //Reduce download speed
echo stream_get_contents($this->file, 2014);
}

fclose( $this->file );
if (defined('XSENDFILE_ENABLE') && XSENDFILE_ENABLE == 1) {
header("X-Sendfile: $filename");
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($save_as));
exit;
} else {
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($save_as));
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Cache-Control: private',false);
header('Content-Length: ' . get_real_size($filename));
header('Connection: close');
//readfile($this->file_location);

$this->context = stream_context_create();
$this->file = fopen($filename, 'rb', false, $this->context);
while ( !feof( $this->file ) ) {
//usleep(1000000); //Reduce download speed
echo stream_get_contents($this->file, 2014);
}

fclose( $this->file );
exit;
}
}
else {
header('Location:' . PAGE_STATUS_CODE_404);
Expand Down
12 changes: 12 additions & 0 deletions includes/forms/options/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@

<div class="options_divide"></div>

<h3><?php _e('Downloads','cftp_admin'); ?></h3>
<div class="form-group">
<div class="col-sm-8 col-sm-offset-4">
<label for="xsendfile_enable">
<input type="checkbox" value="1" name="xsendfile_enable" id="xsendfile_enable" class="checkbox_options" <?php echo (defined('XSENDFILE_ENABLE') && XSENDFILE_ENABLE == 1) ? 'checked="checked"' : ''; ?> /> <?php _e("Use XSendFile to serve files",'cftp_admin'); ?>
<p class="field_note"><?php _e("xsendfile improves downloads by allowing the web server to send the file directly (without php and it's limitations in the middle). This provides several optimizations, such as resumable, more stable downloads. This in an advanced feature that requires you to install and enable a module on your server.",'cftp_admin'); ?></p>
</label>
</div>
</div>

<div class="options_divide"></div>

<h3><?php _e('System location','cftp_admin'); ?></h3>
<p class="text-warning"><?php _e('These options are to be changed only if you are moving the system to another place. Changes here can cause ProjectSend to stop working.','cftp_admin'); ?></p>

Expand Down
2 changes: 2 additions & 0 deletions options.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
case 'general':
$section_title = __('General options','cftp_admin');
$checkboxes = array(
'xsendfile_enable',
'footer_custom_enable',
'files_descriptions_use_ckeditor',
'use_browser_lang',
Expand Down Expand Up @@ -92,6 +93,7 @@
*/
/** Values that can be empty */
$allowed_empty_values = array(
'xsendfile_enable',
'footer_custom_content',
'mail_copy_addresses',
'mail_smtp_host',
Expand Down

0 comments on commit 6f90112

Please sign in to comment.