已經幫你寫好了
<?php
$root="D:/ghost/";
if(is_dir($root)){
$openHandle=opendir($root);
while(false!==($file=readdir($openHandle))){
if(!is_dir($root.$file)){
echo "<a href='?do=down&file=".$file."&path=".urlencode($root)."'>".$root.$file."</a><br/>";
}
}
closedir($openHandle);
}else{
echo "文件夹不存在";
}$do=(isset($_REQUEST['do'])) ? addslashes($_REQUEST['do']) : '';
if($do=='down') {
$file=(isset($_REQUEST['file'])) ? addslashes($_REQUEST['file']) : '';
$path=(isset($_REQUEST['path'])) ? addslashes($_REQUEST['path']) : ''; OB_start();
function DownLoad($info,$file) {
OB_Clean();
if(!file_exists($info)) {
exit('Error:'.__LINE__.','.basename(__FILE__).'!');
}
$info=file_get_contents($info); header('Content-Transfer-Encoding: binary');
header('Content-Type: application/force-download; name="'.$file.'"');
header('Content-Disposition: attachment; filename='.$file.'');
header('Cache-control: private');
header('Pragma: no-cache');
header('Expires: 0');
echo $info;
die();
}
DownLoad($path.$file,$file);
}
<?php
$root="D:/ghost/";
if(is_dir($root)){
$openHandle=opendir($root);
while(false!==($file=readdir($openHandle))){
if(!is_dir($root.$file)){
echo "<a href='?do=down&file=".$file."&path=".urlencode($root)."'>".$root.$file."</a><br/>";
}
}
closedir($openHandle);
}else{
echo "文件夹不存在";
}$do=(isset($_REQUEST['do'])) ? addslashes($_REQUEST['do']) : '';
if($do=='down') {
$file=(isset($_REQUEST['file'])) ? addslashes($_REQUEST['file']) : '';
$path=(isset($_REQUEST['path'])) ? addslashes($_REQUEST['path']) : ''; OB_start();
function DownLoad($info,$file) {
OB_Clean();
if(!file_exists($info)) {
exit('Error:'.__LINE__.','.basename(__FILE__).'!');
}
$info=file_get_contents($info); header('Content-Transfer-Encoding: binary');
header('Content-Type: application/force-download; name="'.$file.'"');
header('Content-Disposition: attachment; filename='.$file.'');
header('Cache-control: private');
header('Pragma: no-cache');
header('Expires: 0');
echo $info;
die();
}
DownLoad($path.$file,$file);
}
header("location:download/excel.csv"); //让电脑自动弹出下载对话框
我寫的這種方式不分文件類型皆可下載,只要對文件有讀取權限就能被下載,所以你在列出要下載的文件的時候要排除掉php類型.
<?php
$root="D:/wamp/www/panjinli/downloadsouce";
if(is_dir($root))
{
$openHandle=opendir($root);
while(false!==($file=readdir($openHandle)))
{
if(!is_dir($root.$file))
{
echo "<a href='download.php?do=down&file=".$file."&path=".urlencode($root)."'>".$root.$file."</a><br/>";
}
}
closedir($openHandle);
}else
{
echo "文件夹不存在";
}
$do=(isset($_REQUEST['do'])) ? addslashes($_REQUEST['do']) : '';
if($do=='down')
{
$file=(isset($_REQUEST['file'])) ? addslashes($_REQUEST['file']) : '';
$path=(isset($_REQUEST['path'])) ? addslashes($_REQUEST['path']) : '';
OB_start();
function DownLoad($info,$file)
{
OB_Clean();
if(!file_exists($info))
{
exit('Error:'.__LINE__.','.basename(__FILE__).'!');
}
$info=file_get_contents($info);
header('Content-Transfer-Encoding: binary');
header('Content-Type: application/force-download; name="'.$file.'"');
header('Content-Disposition: attachment; filename='.$file.'');
header('Cache-control: private');
header('Pragma: no-cache');
header('Expires: 0');
echo $info;
die();
}
DownLoad($path.$file,$file);
}
?>
不行啊 直接把php文件下下来了
我寫的這種方式不分文件類型皆可下載,只要對文件有讀取權限就能被下載,所以你在列出要下載的文件的時候要排除掉php類型.