php 从服务器php下载文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12094080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Download files from server php
提问by Gustavo Benito Silva
I have a URL where I save some projects from my work, they are mostly MDB files, but some JPG and PDF are there too.
我有一个 URL,用于保存我工作中的一些项目,它们主要是 MDB 文件,但也有一些 JPG 和 PDF。
What I need to do is to list every file from that directory (already done) and give the user the option to download it.
我需要做的是列出该目录中的每个文件(已经完成),并为用户提供下载它的选项。
How is that achieved using PHP?
这是如何使用 PHP 实现的?
回答by Mihai Iorga
To read directory contents you can use readdir()and use a script, in my example download.php, to download files
要读取目录内容,您可以使用readdir()并在我的示例中使用脚本download.php下载文件
if ($handle = opendir('/path/to/your/dir/')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "<a href='download.php?file=".$entry."'>".$entry."</a>\n";
}
}
closedir($handle);
}
In download.phpyou can force browser to send download data, and use basename()to make sure client does not pass other file name like ../config.php
在download.php你可以强制浏览器发送下载数据,并使用basename()确保客户端不会传递其他文件名,如../config.php
$file = basename($_GET['file']);
$file = '/path/to/your/dir/'.$file;
if(!file_exists($file)){ // file does not exist
die('file not found');
} else {
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
// read the file from disk
readfile($file);
}
回答by Brian Warshaw
If the folder is accessible from the browser (not outside the document root of your web server), then you just need to output links to the locations of those files. If they are outside the document root, you will need to have links, buttons, whatever, that point to a PHP script that handles getting the files from their location and streaming to the response.
如果可以从浏览器访问该文件夹(不在 Web 服务器的文档根目录之外),那么您只需要输出指向这些文件位置的链接。如果它们在文档根目录之外,您将需要有链接、按钮等,指向一个 PHP 脚本,该脚本处理从它们的位置获取文件并流式传输到响应。
回答by Geordy James
Here is a simpler solution to list all files in a directory and to download it.
这是一个更简单的解决方案,用于列出目录中的所有文件并下载它。
In your index.php file
在你的 index.php 文件中
<?php
$dir = "./";
$allFiles = scandir($dir);
$files = array_diff($allFiles, array('.', '..')); // To remove . and ..
foreach($files as $file){
echo "<a href='download.php?file=".$file."'>".$file."</a><br>";
}
The scandir() function list all files and directories inside the specified path. It works with both PHP 5 and PHP 7.
scandir() 函数列出指定路径内的所有文件和目录。它适用于 PHP 5 和 PHP 7。
Now in the download.php
现在在download.php
<?php
$filename = basename($_GET['file']);
// Specify file path.
$path = ''; // '/uplods/'
$download_file = $path.$filename;
if(!empty($filename)){
// Check file is exists on given path.
if(file_exists($download_file))
{
header('Content-Disposition: attachment; filename=' . $filename);
readfile($download_file);
exit;
}
else
{
echo 'File does not exists on given path';
}
}
回答by Vikas Kandari
Here is the code that will not download courpt files
这是不会下载法院文件的代码
$filename = "myfile.jpg";
$file = "/uploads/images/".$filename;
header('Content-type: application/octet-stream');
header("Content-Type: ".mime_content_type($file));
header("Content-Disposition: attachment; filename=".$filename);
while (ob_get_level()) {
ob_end_clean();
}
readfile($file);
I have included mime_content_type which will return content type of file .
我已经包含了 mime_content_type 它将返回 file 的内容类型。
To prevent from corrupt file download i have added ob_get_level() and ob_end_clean();
为了防止损坏的文件下载,我添加了 ob_get_level() 和 ob_end_clean();

