Dean219 Dean219 - 3 months ago 22
PHP Question

PHP download is 0 bytes from USB, permissions?

im having an issue downloading a file using php. It was working until i recently installed my webserver on a usb stick to test out some new modifications before going live.

Everything worked until i was testing it out on the USB stick to note, however after running the server from the usb stick i was able to download a file, however it was 0 bytes.

I am using a fairly standard download technique that has been documented across the internet :

//test if a parameter was passed
if (isset($_GET['download'])) {
if (!empty($_GET['download'])) {
$files = $_GET['download'];
download($files);
}
}

//download function
function download($files){

if(!file_exists($files)){
die('Error');
}else{
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.basename($path));
header('Content-Type: application/octet-stream');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
ob_start();
flush();
readfile($files);
exit;
}
}


I believed the check to see if the file exists line would prevent the ability to download a file that doesent exist...yet im able to download an empty file.

At the moment im believing that it may be a privileges issue but iv been unable to verify that looking online as the file does exist on both usb and harddrive so maybe it passes the test but isnt allowed to read from usb?

Answer

You are using the wrong variable for checking the file size so you always tell the recipient that the size is zero.

You should have filesize($files) instead.