The perfect implementation code for PHP download function

Source: Internet
Author: User
Tags fread php download ranges
    1. Check if the form is complete ...
    2. if ($form _completed) {
    3. Header ("Location:http://bbs.it-home.org/download/info_check.exe");
    4. Exit
    5. }
    6. ?>
Copy Code

Or the following: To start the download file here, the ID is used to receive the number of the file to download, and then "Redirect" the way to connect to the actual URL.

If you do not want users to directly copy the URL to download the file, you can consider using PHP directly read the actual file and then download the method to implement. 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 if file exists
    4. echo "File not found";
    5. Exit
    6. } else {
    7. $file = fopen ($file _dir. $file _name, "R"); Open File
    8. Input File Label
    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 contents
    14. Echo fread ($file, FileSize ($file _dir. $file _name));
    15. Fclose ($file);
    16. Exit;}
    17. ?>
Copy Code

If the file path is "http" or "FTP" URL, then the source code will be slightly changed, 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. ?>
Copy Code

This allows you to output the file directly with PHP. The above uses the PHP header function, more content can be consulted: PHP file header information detailed.

Code to implement safe download of PHP files

  1. File Security 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. ?>

Copy Code
  • 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.