PHP - Corrupted Downloads

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kast
Forum Newbie
Posts: 1
Joined: Thu Jan 19, 2006 8:36 am

PHP - Corrupted Downloads

Post by kast »

hawleyjr | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Code: Select all

//This is a script being called from another file to download files
//from a non-web accessible directory.  The files download fine
//It's the right size, and everything looks right, but the files
//are corrupted.  Except for .txt files
//PHP Version 4.3.8 Apache 2.0.50
 
 
if ($download) {
$dquery = mysql_query("SELECT file from files where id='$fid'") or die (mysql_error());
 
while ($row = mysql_fetch_array($dquery)) {
 
  $filename = $row[file];
  $dir = '/home/container/files/' . $filename;
  $file = fopen($dir, 'rw'); 
 
 
//set some HTTP headers 
Header('Content-Type: application/x-octet-stream'); 
Header('Content-Transfer-Encoding: binary'); 
Header('Content-Length: ' . filesize($dir)); 
Header('Cache-Control: no-cache, must-revalidate'); //HTTP 1.1 
Header('Cache-Control: post-check=0, pre-check=0', false); //HTTP 1.1 
Header('Pragma: no-cache'); //HTTP 1.0 
Header('Content-Description: Whatever the file is'); 
Header('Content-Disposition: attachment; filename="'.$filename.'"'); 
Header('Title: ' .$filename); 
 
while(!feof($file)) {
     print(fread($file, 4096)); 
 
fclose($file); 
}
}
}
hawleyjr | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

You probably need the b modifier to indicate you want to read binary.. Easier: remove the call to fopen, fread, print and fclose. Simply use http://www.php.net/readfile
Post Reply