【问题标题】:Download files from server php从服务器 php 下载文件
【发布时间】:2012-08-23 14:35:08
【问题描述】:

我有一个 URL,用于保存我工作中的一些项目,它们主要是 MDB 文件,但也有一些 JPG 和 PDF。

我需要做的是列出该目录中的每个文件(已经完成)并让用户选择下载它。

使用 PHP 是如何实现的?

【问题讨论】:

  • 这不是浏览器处理的吗?
  • 但是如何让浏览器开始下载?

标签: php


【解决方案1】:

要读取目录内容,您可以使用 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);
}

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);
}

【讨论】:

  • 我收到 403 消息;检查文件夹权限并设置为 777,所以我不知道发生了什么。
  • 确保路径正常...文件夹和文件的路径
  • 在你的文件所在的目录中检查你的.htaccess文件的内容。
  • 应始终检查文件名是否为有效文件名,并且不包括不应存在的字符。即 ../../../config.php 可能允许下载网站的主配置文件。
【解决方案2】:

这里是不会下载coupt文件的代码

$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);

我已经包含了 mime_content_type,它将返回文件的内容类型。

为了防止文件下载损坏,我添加了 ob_get_level() 和 ob_end_clean();

【讨论】:

    【解决方案3】:

    如果可以从浏览器访问该文件夹(不在 Web 服务器的文档根目录之外),那么您只需输出指向这些文件位置的链接。如果它们在文档根目录之外,您将需要有链接、按钮等指向 PHP 脚本,该脚本处理从它们的位置获取文件并流式传输到响应。

    【讨论】:

    • 如果文件是图像或文本,则不会下载。
    • 如果是在浏览器中打开的文件,可以使用浏览器的保存功能将其保存到文件系统中。任务还是完成了。
    • 如果您可以告诉客户,我了解到大多数consumers 不知道如果他们看到在浏览器中打开的文本文件或 pdf 文件该怎么办..
    【解决方案4】:

    这是一个更简单的解决方案,可以列出目录中的所有文件并进行下载。

    在你的 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>";
    }
    

    scandir() 函数列出指定路径内的所有文件和目录。它适用于 PHP 5 和 PHP 7。

    现在在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';
        }
     }
    

    【讨论】:

      猜你喜欢
      • 2017-10-18
      • 1970-01-01
      • 2018-01-02
      • 1970-01-01
      • 1970-01-01
      • 2018-05-25
      • 1970-01-01
      • 2012-10-13
      • 1970-01-01
      相关资源
      最近更新 更多