Php download code

Source: Internet
Author: User
Tags php download
Php download code

  1. // Check whether all FORM fields are complete...
  2. If ($ form_completed ){
  3. Header ("Location: http://bbs.it-home.org/download/info_check.exe ");
  4. Exit;
  5. }
  6. ?>

Or the following: start to download the file. here, 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.

If you do not want to directly copy the URL to download the file, you can use PHP to directly read the actual file and then download it. The code is as follows:

  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 an "http" or "ftp" URL, the source code will change a little. the code is as follows:

  1. $ File_name = "info_check.exe ";
  2. $ File_dir = "http://bbs.it-home.org /";
  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. ?>

In this way, you can use PHP to directly output files. The above uses the php header function. For more information, see the php file header (header.

Php file secure download code

  1. // Secure File Download

  2. Public function downloads ($ name ){
  3. $ Name_tmp = explode ("_", $ name );
  4. $ Type = $ name_tmp [0];
  5. $ File_time = explode (".", $ name_tmp [3]);
  6. $ File_time = $ file_time [0];
  7. $ File_date = date ("Y/md", $ file_time );
  8. $ File_dir = SITE_PATH. "/data/uploads/$ type/$ file_date /";

  9. If (! File_exists ($ file_dir. $ name )){

  10. Header ("Content-type: text/html; charset = utf-8 ");
  11. Echo "File not found! ";
  12. Exit;
  13. } Else {
  14. $ File = fopen ($ file_dir. $ name, "r ");
  15. Header ("Content-type: application/octet-stream ");
  16. Header ("Accept-Ranges: bytes ");
  17. Header ("Accept-Length:". filesize ($ file_dir. $ name ));
  18. Header ("Content-Disposition: attachment; filename =". $ name );
  19. Echo fread ($ file, filesize ($ file_dir. $ name ));
  20. Fclose ($ file );
  21. }
  22. }
  23. ?>

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.