-
- /**
- * php force download file
- * edit: bbs.it-home.org
- */
- $file_dir = "./";
- $name = "test.txt";
- $file = fopen($file_dir.$name," r");
- Header("Content-type: application/octet-stream");
- Header("Accept-Ranges: bytes");
- Header("Accept-Length: ".filesize($file_dir . $name) );
- Header("Content-Disposition: attachment; filename=".$name);
- echo fread($file, filesize($file_dir.$name));
- fclose($file);
- ?>
Copy the code
The code is very simple. It mainly focuses on learning the principles and implementation methods of forced downloading of files in PHP. You can improve on this basis.
|