PHP-在HTML中创建从本地文件夹获取的所有列表项的下载链接
假设我用PHP编写了以下脚本来创建目录路径/Users/abc/bde/fgh中所有文件的列表。现在我想让他们下载相同的文件链接,我如何才能做到这一点PHP-在HTML中创建从本地文件夹获取的所有列表项的下载链接,php,html,web-services,scripting,server-side,Php,Html,Web Services,Scripting,Server Side,假设我用PHP编写了以下脚本来创建目录路径/Users/abc/bde/fgh中所有文件的列表。现在我想让他们下载相同的文件链接,我如何才能做到这一点 $path = "/Users/abc/bde/fgh"; // Open the folder $dir_handle = @opendir($path) or die("Unable to open $path"); // Loop through the files while ($file = readdir($dir_han
$path = "/Users/abc/bde/fgh";
// Open the folder
$dir_handle = @opendir($path) or die("Unable to open $path");
// Loop through the files
while ($file = readdir($dir_handle)) {
if($file == "." || $file == ".." || $file == "index.php" )
continue;
echo "<a href=\"$file\">$file</a><br />";
}
// Close
closedir($dir_handle);
$path=“/Users/abc/bde/fgh”;
//打开文件夹
$dir_handle=@opendir($path)或die(“无法打开$path”);
//循环浏览文件
而($file=readdir($dir_handle)){
如果($file=>“| |$file==”。“| |$file==”index.php”)
继续;
回声“
”;
}
//接近
closedir($dir_handle);
提前感谢。您正在寻找的可能是强制下载任何文件类型的方法,对吗 看看这段代码,您可能想添加更多的mime类型,这取决于您让人们下载的文件类型 此代码复制自:
您只需要在他们单击下载链接时创建一个新的php页面(或相同的页面),该页面将转到具有文件名参数“file={filename}”的新页面(或相同的页面)。为了安全起见,请不要包含文件路径。此方法存在安全问题,但可能与您无关,这取决于您的情况和下载的内容,以及它是否为公共数据?没有帮助,当我运行代码时,我收到错误消息,称标头已存在。您根本无法向浏览器输出任何内容。确保在您最后的?>语句等之后没有错误、衰减、空格。确保您尚未从其他地方传递头。您还可以搜索“php强制文件下载”以找到类似的解决方案。
// http://davidwalsh.name/php-force-download
// grab the requested file's name
$file_name = $_GET['file'];
// make sure it's a file before doing anything!
if(is_file($file_name)) {
/*
Do any processing you'd like here:
1. Increment a counter
2. Do something with the DB
3. Check user permissions
4. Anything you want!
*/
// required for IE
if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }
// get the file mime type using the file extension
switch(strtolower(substr(strrchr($file_name, '.'), 1))) {
case 'pdf': $mime = 'application/pdf'; break;
case 'zip': $mime = 'application/zip'; break;
case 'jpeg':
case 'jpg': $mime = 'image/jpg'; break;
default: $mime = 'application/force-download';
}
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($file_name)).' GMT');
header('Cache-Control: private',false);
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($file_name)); // provide file size
header('Connection: close');
readfile($file_name); // push it out
exit();
}