• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php实现文件下载功能的几个代码分享_php实例

php 搞代码 3年前 (2022-01-25) 35次浏览 已收录 0个评论

一个简单的php文件下载源代码,虽不支持断点续传等,但是可以满足一些常用的需求了。php下载文件其实用一个a标签就能实现,比如 magento-1.8.1.0.zip 。但是遇到一些浏览器能识别的格式,比如.txt,.html,.pdf等,再用abc.txt 想必也知道会发生什么了。

<?php </P><P>/**<BR> * 文件下载<BR> *<BR>**/</P><P>header("Content-type:text/html;charset=utf-8");<BR>download('web/magento-1.8.1.0.zip', 'magento下载');</P><P>function download($file, $down_name){<BR> $suffix = substr($file,strrpos($file,'.')); //获取文件后缀<BR> $down_name = $down_name.$suffix; //新文件名,就是下载后的名字</P><P> //判断给定的文件存在与否 <BR> if(!file_exists($file)){<BR>  die("您要下载的文件已不存在,可能是被删除");<BR> } <BR> $fp = fopen($file,"r");<BR> $file_size = filesize($file);<BR> //下载文件需要用到的头<BR> header("Content-type: application/octet-stream");<BR> header("Accept-Ranges: bytes");<BR> header("Accept-Length:".$file_size);<BR> header("Content-Disposition: attachment; filename=".$down_name);<BR> $buffer = 1024;<BR> $file_count = 0;<BR> //向浏览器返回数据 <BR> while(!feof($fp) && $file_count < $file_size){<BR>  $file_con = fread($fp,$buffer);<BR>  $file_count += $buffer;<BR>  echo $file_con;<BR> } <BR> fclose($fp);<BR>}</P><P>?><BR>


也可以看看这个注释比较详细的代码:

<?php<BR> //文件下载,下载一张图片<BR> //$file_name="Angel.mp3";</P><P> $file_name="bjnihao.jpg";  //出现中文 程序无法完成下载 提示:文件不存在 <BR> //对文件进行转码(PHP文件函数 比较古老 需对中文码转成 gb2312)<BR> //iconv — Convert string to requested character encoding<BR> //by www.php.net<BR> $file_name=iconv("utf-8","gb2312",$file_name);</P><P> //设置文件下载路径(相对路径)<BR> //$file_path="./dowm/".$file_name;</P><P> //使用绝对路径<BR> $file_path=$_SERVER['DOCUMENT_ROOT']."/http/dowm/".$file_name;</P><P> //打开文件---先判断再操作<BR> if(!file_exists($file_path)){<br><br>  echo "文件不存在";<BR>  return ; //直接退出<BR> }</P><P> //存在--打开文件</P><P> $fp=fopen($file_path,"r");<br><br> //获取文件大小<BR> $file_size=filesize($file_path);</P><P> //http 下载需要的响应头 <BR> header("Content-type: application/octet-stream"); //返回的文件 <BR> header("Accept-Ranges: bytes");   //按照字节大小返回<BR> header("Accept-Length: $file_size"); //返回文件大小<BR> header("Content-Disposition: attachment; filename=".$file_name);//这里客户端的弹出对话框,对应的文件名</P><P> //向客户端返回数据<BR> //设置大小输出<BR> $buffer=1024;</P><P> //为了下载安全,我们最好做一个文件字节读取计数器<BR> $file_count=0;<BR> //判断文件指针是否到了文件结束的位置(读取文件是否结束)<BR> while(!feof($fp) && ($file_size-$file_count)>0){</P><P>  $file_data=fread($fp,$buffer);<BR>  //统计读取多少个字节数<BR>  $file_count+=$buffer;<BR>  //把部分数据返回给浏览器<BR>  echo $file_data;<BR> }<BR> //关闭文件</P><P> fclose($fp);<BR>?><BR>

来4源gaodaimacom搞#代%码*网搞代gaodaima码

封装函数:

<?php
/*
封装函数:
参数说明—-$file_name:文件名
$file_sub_dir:文件下载的子路径
*/
function file_dowm($file_name,$file_sub_dir){
//文件转码
$file_name=iconv(“utf-8″,”gb2312”,$file_name);

//使用绝对路径
$file_path=$_SERVER[‘DOCUMENT_ROOT’].”$file_sub_dir”.$file_name;

//打开文件—先判断再操作
if(!file_exists($file_path)){

echo “文件不存在”;
return ; //直接退出
}

//存在–打开文件

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

//获取文件大小
$file_size=filesize($file_path);
/*
//这里可以设置超过多大不能下载
if($file_size>50){
echo “文件太大不能下载”;
return ;
}*/

//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;

//为了下载安全,我们最好做一个文件字节读取计数器
$file_count=0;
//判断文件指针是否到了文件结束的位置(读取文件是否结束)
while(!feof($fp) && ($file_size-$file_count)>0){

$file_data=fread($fp,$buffer);
//统计读取多少个字节数
$file_count+=$buffer;
//把部分数据返回给浏览器
echo $file_data;
}

//关闭文件
fclose($fp);
}

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

另一个代码:

public function downloads($name){<BR>  $name_tmp = explode("_",$name);<BR>  $type = $name_tmp[0];<BR>  $file_time = explode(".",$name_tmp[3]);<BR>  $file_time = $file_time[0];<BR>  $file_date = date("Y/md",$file_time);<BR>  $file_dir = SITE_PATH."/data/uploads/$type/$file_date/"; <br><br>  if (!file_exists($file_dir.$name)){<BR>   header("Content-type: text/html; charset=utf-8");<BR>   echo "File not found!";<BR>   exit; <BR>  } else {<BR>   $file = fopen($file_dir.$name,"r"); <BR>   Header("Content-type: application/octet-stream");<BR>   Header("Accept-Ranges: bytes");<BR>   Header("Accept-Length: ".filesize($file_dir . $name));<BR>   Header("Content-Disposition: attachment; filename=".$name);<BR>   echo fread($file, filesize($file_dir.$name));<BR>   fclose($file);<BR>  }<BR> }<BR>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php实现文件下载功能的几个代码分享_php实例
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址