Adam M
Adam M

Reputation: 129

PHP file corruption when downloading .jpg files?

When downloading .jpg files via a URL on my site, the file becomes corrupt. I haven't tested a lot of files bu t.pdf's download fine. I'm seeing the following error in the file:

b>Warning: filesize() [function.filesize]: stat failed for sunny.jpg in /home/content/91/4761691/html/pages/download.php on line 89

Line 89 is the filesize() function as seen below...

$ID=stripslashes($_GET['recordid']);
$file = $_GET['file'];
// the absolute path to your directory storing your files.
$path = '../photos/' . $ID . '/';
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
//header("Content-Disposition: attachment; filename=$file");
header('Content-Disposition: attachment;filename="'.$file.'"');
header("Content-Description: File Transfer");
header('Accept-Ranges: bytes');
header('Content-Length: ' . filesize($file));   <========== line 89
readfile($path.$file);

Any ideas on why this is failing and how to address?

Upvotes: 1

Views: 744

Answers (1)

jeromegamez
jeromegamez

Reputation: 3551

You are writing readfile($path.$file); to output the file, so I assume that you should also write filesize($path.$file); to get the file size of the same file.

header('Content-Length: ' . filesize($path.$file));
readfile($path.$file);

Upvotes: 2

Related Questions