Home  >  Article  >  Backend Development  >  The perfect implementation code of php download function

The perfect implementation code of php download function

WBOY
WBOYOriginal
2016-07-25 09:00:371114browse
  1. // Check if the FORM is completely filled out...
  2. if ($form_completed) {
  3. Header("Location: http://bbs.it-home.org/download/info_check.exe ");
  4. exit;
  5. }
  6. ?>
Copy the code

or the following situation: Start downloading 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 URL.

If you don’t want users to directly copy the URL to download the file, you can consider using PHP to directly read the actual file and then download it. code show as below:

  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. // Enter the 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 content
  14. echo fread($file,filesize($file_dir . $file_name));
  15. fclose($file);
  16. exit;}
  17. ?>
Copy the code

And if the file path is an "http" or "ftp" URL, the source code will change slightly, 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 the code

This way you can directly output the file using PHP. The php header function is used above. For more information, please refer to: Detailed explanation of php file header information .

Code to implement safe downloading of php files

  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. ?>

Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn