PHP implementation file download details, _ PHP Tutorial

Source: Internet
Author: User
PHP implementation file download details ,. PHP implementation file download details, 1. php download principle, file download source code: Copy the code as follows: php?file_namehaha.jpg; files to be downloaded $ file_nameiconv (ut PHP implementation file download details,

1. php download schematic

2. file download source code:

The code is as follows:


<? Php
$ File_name = "haha.jpg"; // file to be downloaded
$ File_name = iconv ("UTF-8", "gb2312", "$ file_name ");
$ Fp = fopen ($ file_name, "r +"); // to download a file, you must first open the file and write it to the memory.
If (! File_exists ($ file_name) {// checks whether a file exists.
Echo "the file does not exist ";
Exit ();
}
$ File_size = filesize ("a.jpg"); // you can specify the file size.
// Returned file
Header ("Content-type: application/octet-stream ");
// Return in byte format
Header ("Accept-Ranges: bytes ");
// Returns the file size.
Header ("Accept-Length:". $ file_size );
// The client dialog box appears, with the corresponding file name
Header ("Content-Disposition: attachment; filename =". $ file_name );
// Prevents instantaneous server load increases and reads data in segments
$ Buffer = 1024;
While (! Feof ($ fp )){
$ File_data = fread ($ fp, $ buffer );
Echo $ file_data;
}
// Close the file
Fclose ($ fp );
?>

3. Solution to file encoding problems:

If the file name is Chinese, php functions cannot recognize the Chinese file name. generally, if the program code is UTF-8, php functions are old and can only recognize the Chinese characters encoded in gb2312, therefore, the iconv ("original encoding", "encoding to be converted", and "string to be transcoded") function can be used for transcoding.

For example, transcode a string from UTF-8 to gb2312

$ File_name = iconv ("UTF-8", "gb2312", "$ file_name ");

4. use the header method to download the source code.

The attached download method is encapsulated and can be used directly:

The code is as follows:


Function download_by_path ($ path_name, $ save_name ){
Ob_end_clean ();
$ Hfile = fopen ($ path_name, "rb") or die ("Can not find file: $ path_name \ n ");
Header ("Content-type: application/octet-stream ");
Header ("Content-Transfer-Encoding: binary ");
Header ("Accept-Ranges: bytes ");
Header ("Content-Length:". filesize ($ path_name ));
Header ("Content-Disposition: attachment; filename = \" $ save_name \"");
While (! Feof ($ hfile )){
Echo fread ($ hfile, 32768 );
}
Fclose ($ hfile );
}

The above is all the content in this article. do you have a preliminary understanding of php file download? I will try more on my own and use the simple example given in this article, make your project more complete.

Example, 1. php download schematic 2. file download source code: the code is as follows: php $ file_name = "..jpg"; // the file to be downloaded $ file_name = iconv ("ut...

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.