PHP to achieve the download function of the code

  • 2020-05-26 07:54:30
  • OfStack

wzskynet#163.com
· php escapeshellcmd multi-byte encoding vulnerability
· explain the application of caching technology in PHP in detail
· develop multitasking applications using PHP V5
· detail the process of sending data from PHP to MySQL
· a brief discussion on the method of PHP to realize static publishing
Would you laugh at the simplicity of "downloading files"? It's not as simple as you might think. For example, if you want the customer to complete a form before downloading a file, your first idea is to use the method of "Redirect" to check whether the form has been completed and complete, and then point the url to the file so that the customer can download it. For example, the following code is written by the author:
 
<? 
//  check  FORM  Have you completed all of them ... 
if ($form_completed) { 
Header("Location: https://www.ofstack.com/download/info_check.exe"); 
exit; 
} 
?> 

Or the following:
 
<a href="http://www.yourwebl.com/users/download.php?id=124524"> Start downloading files </a> 

This USES ID to receive the number of the file to be downloaded, and then "Redirect" to connect to the actual url.

If you want to make an e-commerce website about "online shopping", for security reasons, you do not want users to directly copy the url to download the file, I suggest you use PHP directly read the actual file and then download the method to do. The procedure is as follows:
 
<? 
$file_name = "info_check.exe"; 
$file_dir = "/public/www/download/"; 
if (!file_exists($file_dir . $file_name)) { // Check if the file exists  
echo " File not found "; 
exit; 
} else { 
$file = fopen($file_dir . $file_name,"r"); //  Open the file  
//  Input 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;} 
?> 

If the file path is "http" or "ftp", the source code will be slightly changed as follows:
 
 The < ? 
$file_name = "info_check.exe"; 
$file_dir = "https://www.ofstack.com/"; 
$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); 
} 
? >  

This allows you to output files directly using PHP.

php file security 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); 
} 
} 

Related articles: