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文檔,就會直接在瀏覽器中打開
而第二種方法是直接輸出的文件流,不存在上述問題