PHP download problem, after downloading the contents of the file is empty, the picture is not previewed

Source: Internet
Author: User
Tags fread php download ranges
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.
 
  


This is all the code for b.php.
 
  


Reply to discussion (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");

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");

Yes, but I changed it to the same thing. The result code is as follows

 
  

You can't change the font color in the code, OH, again.

 
  

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);    }

Success, the first to thank the moderator, the moderator said very good, and then must share:
is to download compressed files, including files in the compressed file, the code is not difficult

Open File $file = fopen ($file _dir. $file _name, "R");//file type to return header ("Content-type:application/octet-stream");// Returns the header ("Accept-ranges:bytes") by byte size,//Returns the size of the file header ("Accept-length:". FileSize ($file _dir. $file _name));// Here the popup dialog box for the client, corresponding to the filename header ("content-disposition:attachment; Filename= ". $file _dir. $file _name);//Modify the data to the client echo fread ($file, FileSize ($file _dir. $file _name) before modifying, Transmits only 1024 bytes of data at a time to the client//client loopback data $buffer=1024;////determine if the file reads while (!feof ($file)) {//reads the file into memory $file_data=fread ($file, $ buffer);//Echo 1024 bytes of data to the client echo $file _data;} Fclose ($file);
  • 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: info-contact@alibabacloud.com 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.