-
- // Check if the FORM is completely filled out...
- if ($form_completed) {
- Header("Location: http://bbs.it-home.org/download/info_check.exe ");
- exit;
- }
- ?>
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:
-
- $file_name = "info_check.exe";
- $file_dir = "/public/www/download/";
- if (!file_exists($file_dir . $file_name)) { //Check Whether the file exists
- echo "File not found";
- exit;
- } else {
- $file = fopen($file_dir . $file_name,"r"); // Open the file
- // Enter the file label
- Header(" Content-type: application/octet-stream");
- Header("Accept-Ranges: bytes");
- Header("Accept-Length: ".filesize($file_dir . $file_name));
- Header("Content- Disposition: attachment; filename=" . $file_name);
- // Output file content
- echo fread($file,filesize($file_dir . $file_name));
- fclose($file);
- exit;}
- ?>
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:
-
- $file_name = "info_check.exe";
- $file_dir = "http://bbs.it-home.org/";
- $file = @fopen($file_dir . $ file_name,"r");
- if (!$file) {
- echo "File not found";
- } else {
- Header("Content-type: application/octet-stream");
- Header("Content- Disposition: attachment; filename=" . $file_name);
- while (!feof ($file)) {
- echo fread($file,50000);
- }
- fclose ($file);
- }
- ?>
-
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
-
-
//Secure file download - public function downloads($name){
- $name_tmp = explode("_",$name);
- $type = $name_tmp [0];
- $file_time = explode(".",$name_tmp[3]);
- $file_time = $file_time[0];
- $file_date = date("Y/md",$file_time);
- $file_dir = SITE_PATH."/data/uploads/$type/$file_date/";
if (!file_exists($file_dir.$name)){
- header("Content-type: text/ html; charset=utf-8");
- echo "File not found!";
- exit;
- } else {
- $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 code
|