Simple analysis of php file download principle _php Example

Source: Internet
Author: User
Tags php download

1, php download schematic diagram

2, File download Source:

Copy Code code as follows:

<?php
$file _name= "haha. jpg";//Files to download
$file _name=iconv ("Utf-8", "gb2312", "$file _name");
$FP =fopen ($file _name, "r+");//download file must first open file, write to memory
if (!file_exists ($file _name)) {//Determine if the file exists
echo "file does not exist";
Exit ();
}
$file _size=filesize ("a.jpg");//Determine File size
The file returned
Header ("Content-type:application/octet-stream");
return in byte format
Header ("Accept-ranges:bytes");
Return file size
Header ("Accept-length:". $file _size);
Pop-up Client dialog box, corresponding filename
Header ("content-disposition:attachment; Filename= ". $file _name);
To prevent the server from increasing instantaneous pressure, segmented read
$buffer = 1024;
while (!feof ($fp)) {
$file _data=fread ($fp, $buffer);
echo $file _data;
}
Close File
Fclose ($FP);
?>

3, File coding problem Solving method:
If the file name is Chinese, PHP functions can not recognize the Chinese file name, generally if the program code for the utf-8,php function is relatively old, can only identify gb2312 encoded in Chinese, so the Chinese with Iconv ("Original Code", "to convert to the code", "to transcoding the string") function to transcoding.

For example, turn a string from Utf-8 to gb2312

$file _name=iconv ("Utf-8", "gb2312", "$file _name");

Related Article

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.