I made a backup of my server (Ghost drive-dump). Now, the server is a bit restricted in its configuration, so to move the backup file from the server, I tried to download using the admin file controls. I don't have FTP and I have turned off most other alternatives, like local network.
The admin file-controls-download is OK for small files, but it didn't work with my "bigger" file.
/* Download file code. */
if (isset($_GET['down']) && $dest && @file_exists($cur_dir .'/'. $dest)) {
if (is_file($cur_dir .'/'. $dest)) {
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename='. $dest);
readfile($cur_dir .'/'. $dest);
} else {
header('Content-type: application/x-tar');
header('Content-Disposition: attachment; filename='. $dest .'.tar');
echo make_tar($cur_dir .'/'. $dest);
}
exit;
}
The PHP cannot run a large file by "readfile". The "readfile" is a problematic spot on the php, and you find many fixes, problems and issues on the www.php.net
I just need the file so I put in the following fix:
- I included a function that send the file in "chunks".
- I supply the file length to the download browser.
- I disable (go around) the php timeout by sending "sleep( 0);"
Insert this function in the adm/admbrowse.php file, among the other functions:
function readfile_chunked( $filename, $retbytes=true)
{
$chunksize = 10*1024*1024; // how many bytes per chunk
$buffer = '';
$cnt =0;
$handle = fopen($filename, 'rb');
if ( $handle === false)
{
return false;
}
while ( !feof( $handle))
{
$buffer = fread( $handle, $chunksize);
echo $buffer;
ob_flush();
flush();
sleep( 0);
if ( $retbytes)
{
$cnt += strlen($buffer);
}
}
$status = fclose( $handle);
if ( $retbytes && $status)
{
return $cnt; // return num. bytes delivered like readfile() does.
}
return $status;
}
And then modify the file download to use this function (the function above):
/* Download file code. */
if (isset($_GET['down']) && $dest && @file_exists($cur_dir .'/'. $dest)) {
if (is_file($cur_dir .'/'. $dest)) {
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename='. $dest);
header('Content-Length: '. filesize( $cur_dir .'/'. $dest));
// readfile($cur_dir .'/'. $dest);
readfile_chunked( $cur_dir .'/'. $dest);
} else {
header('Content-type: application/x-tar');
header('Content-Disposition: attachment; filename='. $dest .'.tar');
echo make_tar($cur_dir .'/'. $dest);
}
exit;
}
#############################
We note that I cannot upload the backup file (that is large..) again when it has been downloaded. The upload would need a similar adjustment.
I have not included the fix into the directory download.