php Implements Code Sharing for File Download

  • 2021-06-28 08:50:22
  • OfStack

A simple php file downloads the source code. Although breakpoint continuation is not supported, it can meet some common requirements.php download files can be achieved with an a tag, such as < a href="web/magento-1.8.1.0.zip" > magento-1.8.1.0.zip < /a > .But there are a few formats that browsers can recognize, such as.txt,.html,.pdf, and so on. < a href="web/abc.txt" > abc.txt < /a > You must know what's going to happen.


<?php 
/**
 *  File Download 
 *
**/
header("Content-type:text/html;charset=utf-8");
download('web/magento-1.8.1.0.zip', 'magento download ');
function download($file, $down_name){
 $suffix = substr($file,strrpos($file,'.')); // Get File Suffix 
 $down_name = $down_name.$suffix; // The new file name is the downloaded name 
 // Determine whether a given file exists or not  
 if(!file_exists($file)){
  die(" The file you are downloading no longer exists, it may have been deleted ");
 } 
 $fp = fopen($file,"r");
 $file_size = filesize($file);
 // Headers needed to download files 
 header("Content-type: application/octet-stream");
 header("Accept-Ranges: bytes");
 header("Accept-Length:".$file_size);
 header("Content-Disposition: attachment; filename=".$down_name);
 $buffer = 1024;
 $file_count = 0;
 // Return data to browser  
 while(!feof($fp) && $file_count < $file_size){
  $file_con = fread($fp,$buffer);
  $file_count += $buffer;
  echo $file_con;
 } 
 fclose($fp);
}
?>


You can also see the more detailed code in this comment:


<?php
 //文件下载,下载1张图片
 //$file_name="Angel.mp3";
 $file_name="bjnihao.jpg";  //出现中文 程序无法完成下载 提示:文件不存在 
 //对文件进行转码(PHP文件函数 比较古老 需对中文码转成 gb2312)
 //iconv ― Convert string to requested character encoding
 //by www.ofstack.com
 $file_name=iconv("utf-8","gb2312",$file_name);
 //设置文件下载路径(相对路径)
 //$file_path="./dowm/".$file_name;
 //使用绝对路径
 $file_path=$_SERVER['DOCUMENT_ROOT']."/http/dowm/".$file_name;
 //打开文件---先判断再操作
 if(!file_exists($file_path)){

  echo "文件不存在";
  return ; //直接退出
 }
 //存在--打开文件
 $fp=fopen($file_path,"r");

 //获取文件大小
 $file_size=filesize($file_path);
 //http 下载需要的响应头 
 header("Content-type: application/octet-stream"); //返回的文件 
 header("Accept-Ranges: bytes");   //按照字节大小返回
 header("Accept-Length: $file_size"); //返回文件大小
 header("Content-Disposition: attachment; filename=".$file_name);//这里客户端的弹出对话框,对应的文件名
 //向客户端返回数据
 //设置大小输出
 $buffer=1024;
 //为了下载安全,我们最好做1个文件字节读取计数器
 $file_count=0;
 //判断文件指针是否到了文件结束的位置(读取文件是否结束)
 while(!feof($fp) && ($file_size-$file_count)>0){
  $file_data=fread($fp,$buffer);
  //统计读取多少个字节数
  $file_count+=$buffer;
  //把部分数据返回给浏览器
  echo $file_data;
 }
 //关闭文件
 fclose($fp);
?>

Encapsulation function:

< ?php
/*
Encapsulation function:
Parameter Description - $file_name: File name
$file_sub_dir: Subpath of file download
*/
function file_dowm($file_name,$file_sub_dir){
//File Transcoding
$file_name=iconv("utf-8","gb2312",$file_name);

//Use absolute paths
$file_path=$_SERVER['DOCUMENT_ROOT']."$file_sub_dir".$file_name;

//Open file - judge before you operate
if(!file_exists($file_path)){

echo "File does not exist";
return; //immediate withdrawal
}

//Exist--Open File

$fp=fopen($file_path,"r");

//Get file size
$file_size=filesize($file_path);
/*
//Here you can set how big you cannot download
if($file_size > 50){
echo "File too big to download";
return ;
}*/

//http Download Response Header Required
header ("Content-type: application/octet-stream"); //Files returned
header ("Accept-Ranges: bytes"); //Return by byte size
header ("Accept-Length: $file_size ";//Return file size
header ("Content-Disposition: attachment;filename=". $file_name);//Client's pop-up dialog here, corresponding file name

//Return data to client
//Set Size Output
$buffer=1024;

//For download security, we'd better make a file byte read counter
$file_count=0;
//Determine if the file pointer reached the end of the file (end of reading file)
while(!feof($fp) & & ($file_size-$file_count) > 0){

$file_data=fread($fp,$buffer);
//Count how many bytes read
$file_count+=$buffer;
//Return some data to the browser
echo $file_data;
}

//Close File
fclose($fp);
}

file_dowm("bjnihao.jpg","/http/dowm/");
? >

Another code:


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: