Php file download method

Source: Internet
Author: User
Php file download method

  1. // Check whether all form fields are complete...
  2. If ($ form_completed ){
  3. Header ("Location: http://www.myweb.com/download/info_check.exe ");
  4. Exit;
  5. }
  6. ?>
  7. Or:
  8. Start file download

The ID method is used to receive the number of the file to be downloaded, and then the "Redirect" method is used to connect to the actual website.

For security reasons, users are not allowed to directly copy the website to download the file. we recommend that you use PHP to directly read the actual file and then download it.

Code:

  1. $ File_name = "info_check.exe ";
  2. $ File_dir = "/public/www/download /";
  3. If (! File_exists ($ file_dir. $ file_name) {// check whether the file exists
  4. Echo "file not found ";
  5. Exit;
  6. } Else {
  7. $ File = fopen ($ file_dir. $ file_name, "r"); // open the file
  8. // Input file tag
  9. Header ("Content-type: application/octet-stream ");
  10. Header ("Accept-Ranges: bytes ");
  11. Header ("Accept-Length:". filesize ($ file_dir. $ file_name ));
  12. Header ("Content-Disposition: attachment; filename =". $ file_name );
  13. // Output file content
  14. Echo fread ($ file, filesize ($ file_dir. $ file_name ));
  15. Fclose ($ file );
  16. Exit ;}
  17. ?>

If the file path is "http" or "ftp", the source code will change a little. The program is as follows:

  1. $ File_name = "info_check.exe ";
  2. $ File_dir = "http://www.jincai123.com /";
  3. $ File = @ fopen ($ file_dir. $ file_name, "r ");
  4. If (! $ File ){
  5. Echo "file not found ";
  6. } Else {
  7. Header ("Content-type: application/octet-stream ");
  8. Header ("Content-Disposition: attachment; filename =". $ file_name );
  9. While (! Feof ($ file )){
  10. Echo fread ($ file, 50000 );
  11. }
  12. Fclose ($ file );
  13. }
  14. ?>

With the above code, you can use php to directly output files.

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.