php下载多个文件
㈠ php如何下载文件
//下载文件文件用绝对路径
ob_clean();
header('Pragma:public');
header('Last-Modified:'.gmdate('D,dMYH:i:s').'GMT');
header('Cache-Control:no-store,no-cache,must-revalidate');
header('Cache-Control:pre-check=0,post-check=0,max-age=0');
header('Content-Transfer-Encoding:binary');
header('Content-Encoding:none');
header('Content-type:multipart/form-data');
header('Content-Disposition:attachment;filename="'.$filename.'"');//设置下载的默认文件名
header('Content-length:'.filesize($dfile));//要下载的文件
$fp=fopen($dfile,'r');
while(connection_status()==0&&$buf=@fread($fp,8192)){
echo$buf;
}
fclose($fp);
@unlink($dfile);
@flush();
@ob_flush();
exit();
㈡ php实现文件下载代码~
echo "<a href='$file'>".$file."</a>"需要绝对路径,而且注意目录是不可以下载的。
<?php
$root="d:/";
if(is_dir($root)){
$openHandle=opendir($root);
while(false!==($file=readdir($openHandle))){
if(!is_dir($root.$file))
echo "<a href='$root$file'>".$file."</a><br/>";
}
closedir($openHandle);
}
else {
echo "文件夹不存在";
}
?>
㈢ 怎样用多文件php实现多文件下载
好可怜啊,谁给你的任务,这个是不可能的,和框架、语言都没关系,是不是你的上级看错了文档啊,人家要求多个文件用zip打包下载(比如把文档中的“别忘了用zip”看成“别用zip”)
这种只能js同时弹出多窗口了,例如
<formclass="download_form"target="download_url_1"action="download_url_1"></form>
<formclass="download_form"target="download_url_2"action="download_url_2"></form>
...
<buttononclick="$('form.download_form').each(function(){this.submit();})"type="button">点</button>
只要下载是正常的,这种弹出会在用户操作后(下载或取消)自动消失的。但是用户要一个一个点还是很差劲的用户体验。
window.open也行,但是容易受浏览器弹窗设置影响,所以不建议。
㈣ php中让文件循环下载的代码怎么写
自己写的方法 但是在中文路径下会 出现错误
/*
*查看问价夹中的子文件及其子文件夹
*$path 付文件夹路径
*return $arr_dir 文件夹中所有文件和子文件夹的信息
*/
function selDir($path){
$arr_dir = array();
if(is_file($path)){
header("Location: error.php");
exit;
}
$arr_dir = scandir($path);
foreach ($arr_dir as $v){
if($v != "." && $v != ".."){
//print "<hr>".$path.$v;
if(is_dir($path.'/'.$v)){
//print "<hr>是文件夹<hr>";
$arr_dir['dir'][] = array(
'fileUrl' => $path.'/'.$v,
'filename' => $v,
'type' => '文件夹',
'cTime' => @date('Y/m/d H:i',filectime($path.'/'.$v)+8*3600),
'mTime' => @date('Y/m/d H:i',filemtime($path.'/'.$v)+8*3600),
'filesize' => ''
);
}else{
//print "<hr>不是是文件夹<hr>";
$arr_dir['file'][] = array(
'fileUrl' => $path.'/'.$v,
'filename' => $v,
'type' => pathinfo($path.'/'.$v, PATHINFO_EXTENSION),
'cTime' => @date('Y/m/d H:i',filectime($path.'/'.$v)+8*3600),
'mTime' => @date('Y/m/d H:i',filemtime($path.'/'.$v)+8*3600),
'filesize' => filesize($path.'/'.$v)
);
}
}
}
//var_mp($arr_dir);
return $arr_dir;
}
㈤ PHP实现文件下载
2种方法
1直接做个超链接,地址为文件的地址
<ahref="文件地址">下载</a>
2流输出
<?php
$file=fopen('文件地址',"r");
header("Content-Type:application/octet-stream");
header("Accept-Ranges:bytes");
header("Accept-Length:".filesize('文件地址'));
header("Content-Disposition:attachment;filename=文件名称");
echofread($file,filesize('文件地址'));
fclose($file);
?>
推荐第二种
因为第一种方法只能下载浏览器不能解析的文件,比如rar啊,脚本文件之类。如果文件是图片或者txt文档,就会直接在浏览器中打开
而第二种方法是直接输出的文件流,不存在上述问题