PHP download problem, after downloading the contents of the file is empty, the picture is not previewed
There are two files, a.php and b.php
Click the link in a.php to start downloading the file: The code is as follows
This is a.php.
$file _dir = "D:/files";
$file _name = $row [1]; This is from the database.
echo "". $row [2]. ""; Click this link, download the file name is right, the file is not content, if it is a picture, the picture is not previewed
?>
This is all the code for b.php.
$file _name = $_get[' file_name '); Filename
$file _dir = $_get[' File_dir '); File path
if (!file_exists ($file _dir. $file _name)) {
echo "Could not find [". $file _dir. $file _name. "] Document";
Exit
}else{
$file =fopen ($file _dir. $file _name, "R");
Header ("Content-type:application/octet-stream");
Header ("Accept-ranges:bytes");
Header ("Accept-length:". FileSize ($file _dir. $file _name));
Header ("content-disposition:attachment; Filename= ". $file _name);
}
?>
Share to:
------Solution--------------------
a.php is $file _dir = "D:/files";
Then in the b.php of the $file _dir. $file _name is missing a path character
You have only $file =fopen in b.php ($file _dir $file _name, "R"); Opened the file, but did not output the contents of the file
If using $file =fopen ($file _dir. $file _name, "R"); The open file is a picture, you will not be able to read the picture data correctly
Should be opened in binary mode: $file =fopen ($file _dir. $file _name, "R b");
------Solution--------------------
This post was last edited by xuzuning on 2013-06-08 16:08:01
}else{
$file = $file _dir. $file _name;
Header ("Content-type:application/octet-stream");
Header ("Accept-ranges:bytes");
Header ("Accept-length:". FileSize ($file));
Header ("content-disposition:attachment; Filename= ". $file _name);
ReadFile ($file);
}