Home >Backend Development >PHP Tutorial >这样为什么不能实现文件下载解决方案

这样为什么不能实现文件下载解决方案

WBOY
WBOYOriginal
2016-06-13 11:49:011007browse

这样为什么不能实现文件下载
$root="D:/wamp/www/新建文件夹/";
if(is_dir($root))
{
   $openHandle=opendir($root);
   while(false!==($file=readdir($openHandle)))
   {
   if(!is_dir($root.$file))
   {
   echo "".$file."
";
    echo "$root$file
";
   }
   }
   closedir($openHandle);
}
else 
{
echo "文件夹不存在";
}
?>
这段代码为什么不能实现文件下载 浏览器:搜狗浏览器 IE 都试了
------解决方案--------------------
用header头下载
------解决方案--------------------
已經幫你寫好了

<?php<br />$root="D:/ghost/";<br />if(is_dir($root)){<br />   $openHandle=opendir($root);<br />   while(false!==($file=readdir($openHandle))){<br />	   if(!is_dir($root.$file)){<br />		 echo "<a href='?do=down&file=".$file."&path=".urlencode($root)."'>".$root.$file."</a><br/>";<br />	   }<br />   }<br />   closedir($openHandle);<br />}else{<br />echo "文件夹不存在";<br />}<br /><br />$do=(isset($_REQUEST['do'])) ? addslashes($_REQUEST['do']) : '';<br />if($do=='down') {<br />	$file=(isset($_REQUEST['file'])) ? addslashes($_REQUEST['file']) : '';<br />	$path=(isset($_REQUEST['path'])) ? addslashes($_REQUEST['path']) : '';<br /><br />	OB_start();<br />	function DownLoad($info,$file) {<br />		OB_Clean();<br />		if(!file_exists($info)) {<br />			exit('Error:'.__LINE__.','.basename(__FILE__).'!');<br />		}<br />		$info=file_get_contents($info);<br /><br />		header('Content-Transfer-Encoding: binary');<br />		header('Content-Type: application/force-download; name="'.$file.'"');<br />		header('Content-Disposition: attachment; filename='.$file.'');<br />		header('Cache-control: private');<br />		header('Pragma: no-cache');<br />		header('Expires: 0');<br />		echo $info;<br />		die();<br />	}<br />	DownLoad($path.$file,$file);<br />}

------解决方案--------------------
file_put_contents('download/excel.csv',$str);
header("location:download/excel.csv"); //让电脑自动弹出下载对话框

Statement:
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