Skip to content

Commit

Permalink
ncp-web: fix backup download for big files in 32-bit
Browse files Browse the repository at this point in the history
Signed-off-by: nachoparker <[email protected]>
  • Loading branch information
nachoparker committed May 13, 2021
1 parent be30663 commit 0ee3aa9
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions ncp-web/download.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@
if (!is_readable($file))
die('NCP does not have read permissions on this file');

$size = filesize($file);
function filesize_compat($file)
{
if(PHP_INT_SIZE === 4) # workaround for 32-bit architectures
return trim(shell_exec("stat -c%s " . escapeshellarg($file)));
else
return filesize($file);
}

$size = filesize_compat($file);

$extension = pathinfo($file, PATHINFO_EXTENSION);
if ($extension === "tar" )
Expand All @@ -41,28 +49,29 @@
ob_clean();
header('Content-Description: File Transfer');
header('Content-Type: ' . $mime_type);
header("Content-Transfer-Encoding: Binary");
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file) . "\"");
header('Content-Length: ' . $size);
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');

$chunksize = 8 * (1024 * 1024);
if($size > $chunksize)

if($size > $chunksize || PHP_INT_SIZE === 4) # always chunk for 32-bit architectures
{
$handle = fopen($file, 'rb') or die("Error opening file");

while (!feof($handle))
{
{
$buffer = fread($handle, $chunksize);
echo $buffer;

ob_flush();
flush();
}
}

fclose($handle);
fclose($handle);
}
else
readfile($file);
Expand Down

0 comments on commit 0ee3aa9

Please sign in to comment.