Home > Backend Development > PHP Tutorial > PHP uses code to download files

PHP uses code to download files

不言
Release: 2023-03-25 06:02:01
Original
1607 people have browsed it

This article mainly introduces the use of PHP code to download files. It has certain reference value. Now I share it with everyone. Friends in need can refer to it

<?
if( empty($_GET[&#39;FileName&#39;])|| empty($_GET[&#39;FileDir&#39;])|| empty($_GET[&#39;FileId&#39;])){ 
  echo&#39;<script> alert("非法连接 !"); location.replace ("index.php") </script>&#39;;
  exit();
}
$file_name=$_GET[&#39;FileName&#39;];
$file_dir=$_GET[&#39;FileDir&#39;];
$FileId=$_GET[&#39;FileId&#39;];
$file_dir = $file_dir."/";
if   (!file_exists($file_dir.$file_name))
   {  
    //检查文件是否存在  
  echo   "文件找不到";  
  exit;    
  }   else   {  
$file = fopen($file_dir . $file_name,"r"); // 打开文件// 输入文件标签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);// 输出文件内容echo fread($file,filesize($file_dir . $file_name));
fclose($file);exit();
}
Copy after login

Related recommendations:

PHP uses the parabolic model to implement the program source code of WeChat red envelope generation algorithm


The above is the detailed content of PHP uses code to download files. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template