Why is the downloaded file corrupted? Function f ($ file_name, $ file_path ){
$ File_name = $ _ SERVER ['document _ root']. $ file_path. $ file_name;
$ File_path = iconv ("UTF-8", "gb2312", $ file_name );
If (! File_exists ($ file_path ))
// If (! File_exists ($ file_name ))
{
Echo "does not exist ";
Return;
}
$ Fp = fopen ($ file_path, "r ");
$ File_size = filesize ($ file_path );
Header ("Content-type: application/octet-stream ");
Header ("Accept-Ranges: bytes ");
Header ("Accept-Length: $ file_size ");
Header ("Content-Disposition: attachment; filename =". $ file_name );
$ Buffer = 1024;
While (! Feof ($ fp ))
{
$ File_data = fread ($ fp, $ buffer );
Echo $ file_data;
}
Fclose ($ fp );
}
F ("qq.jpg ","/");
Why is the downloaded file damaged ??
Why is the downloaded file damaged ??
Why is the downloaded file damaged ??
Why is the downloaded file damaged ??
Why is the downloaded file damaged ??
Reply to discussion (solution)
Tested, no problem.
Why can't I?
Thunder or something? The downloaded files may be incomplete.
It's okay to open the picture. it's not possible to download the tested code.
Take a look at the screenshot.
In addition, what do you use to open it. What is the code runtime environment?
Tested, everything works.
Image Viewer for win7
Google Chrome apache php5.3
Are there any qq.jpg files in the E/Apache/htdocs directory?
Yes, you cannot see it ....
Open the image file in notepad to see what is there.
A bunch of garbled characters
Your code includes $ file_path = iconv ("UTF-8", "gb2312", $ file_name );
Indicates that your php works in the window system.
The window system strictly distinguishes between text files and binary files.
However, you can use $ fp = fopen ($ file_path, "r") to open binary files in text mode.
So, because you violate the rules, you cannot get the correct result.
You need to open the binary file $ fp = fopen ($ file_path, "r B") in binary mode ");
In addition, your UTF-8 encoded program files must be saved in a format without a BOM header.
Thanks. you can save it in the format without BOM header.