Do not use the database to implement the user's simple download, the code is as follows, but can not download, please master to find the reason, file road strength what no problem

Source: Internet
Author: User
Do not use the database to implement the user's simple download, the code is as follows, but can not download, please master to find the reason, the file road strength or anything.

function Down_file ($file _name, $file _sub_dir) {



To prevent garbled use of function iconv
$file _name=iconv ("Utf-8", "gb2312", $file _name);

Absolute Road Strength
$file _path=$_server[' Document_root '). $file _sub_dir. $file _name;

Open File
if (!file_exists ($file _path)) {
echo "file does not exist";
Return

}
$FP =fopen ($file _path, "R");

Get the size of a file
$file _size=filesize ($file _path);


Limit Download size 10M
if ($file _size>10*1021*1024) {

echo "";
Return

}
Returned file properties This header tells the browser to download
Header ("Content-type:application/octet-stream");
Returns by byte size
Header ("Accept-ranges:bytes");
Return file size
Header ("Accept-length: $file _size");
Here the Client popup dialog box, corresponding to the file name
Header ("content-dispositon:attachment; Filename= ". $file _name);

Sending data to the client
$buffer = 1024;
In order to download security, we'd better make a file ourselves eh read counter
$file _count=0;
This sentence is used to determine whether the file ends
while (!feof ($fp) && ($file _size-$file _count>0)) {

$file _data=fread ($fp, $buffer);
How many bytes did the statistic read?
$file _count+= $buffer;
Return part of the data to the browser
echo $file _data;


}
Close File
Fclose ($FP);





}






?>

------Solution--------------------
Look carefully before you find out.
Header ("content-disposition:attachment; Filename= ". $file _name);
I'm missing one.
  • 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.