php implementation file download can be more introduced

  • 2020-05-26 08:05:37
  • OfStack

PHP USES code to download files, read PHP USES code to download files, we generally download the implementation is called url to download, but encounter ie can recognize open files can not use this way, such as download a picture, html web page, then need to be programmed to achieve, php code can solve the following: if(empty($_GET) [we usually call url to download, but we can't use this method when ie can recognize open files, such as downloading a picture, html web page, etc., then we need to program to achieve, the following php code can be solved:
 
<? 
if( empty($_GET['FileName'])|| empty($_GET['FileDir'])|| empty($_GET['FileId'])){ 
echo'<script> alert(" Illegal connection  !"); location.replace ("index.php") </script>'; exit(); 
} 
$file_name=$_GET['FileName']; 
$file_dir=$_GET['FileDir']; 
$FileId=$_GET['FileId']; 
$file_dir = $file_dir."/"; 
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(); 
} 
?> 

Related articles: