This is a file download code, why the file after the download is always empty solution

Source: Internet
Author: User
This is a file download code, why the file is always empty after download
function Down_file ($file _name, $file _sub_dir) {
$file _name=iconv ("Utf-8", "gb2312", $file _name);
$file _path=$_server[' Document_root '). $file _sub_dir. $file _name;
Open File
if (!file_exists ($file _path)) {
echo "File does not exist, sorry";
$FP =fopen ($file _path, "R");
Get the size of the file to download
$file _size=filesize ($file _path);
Set the returned file
Header ("Content-type:application/octet-stream");
Sets the unit of return file, here is the byte
Header ("Accept-range:bytes");
Set the size of the returned file
Header ("Accept-length: $file _size");
Set the name of the file to be displayed when the Client popup dialog box
Header ("Content-disposition:attachment;filename= $file _name");
Loopback data to the client (sub-loopback)
Set the size of the loopback data each time
$buffer = 1024;
Set a file byte read counter to ensure the security of the download
$file _count=0;
while ((!feof ($FP)) && ($file _size-$file _count > 0)) {
$file _data=fread ($fp, $file _size);
$file _count + = $buffer;
Fclose ($FP);

Down_file ("Sunset.jpg", "/filedown/down/");

You don't have echo file content
$file _data=fread ($fp, $file _size);
You just read the data from the file.
But the data is not output
echo $file _data;

PHP Code
while ((!feof ($FP)) && ($file _size-$file _count > 0)) {  $file _data=fread ($fp, $file _size);  $file _count + = $buffer;  } [B]print ($file _data); [/b] ...
$file _data=fread ($fp, $file _size);
$file _count + = $buffer;

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.