【问题标题】:How to download a file on clicking the name of file using PHP?如何使用 PHP 单击文件名下载文件?
【发布时间】:2011-05-29 22:56:28
【问题描述】:

我有一个信息列表,其中有一个文件名被超链接的字段。我希望用户在单击该特定文件时下载该文件。

那么,如何使用 PHP 点击文件名来下载文件?

我用ajax试了一下,代码如下,但我没有下载任何文件。

download.php

$filename = $_GET['val'];
         // Fetch the file info.
 $filePath = $_SERVER['DOCUMENT_ROOT'] . "/dfms/images/docs/".$filename;

    if(file_exists($filePath)) {
        $fileName = basename($filePath);
        $fileSize = filesize($filePath);

        // Output headers.
        header("Cache-Control: private");
        header("Content-Type: application/stream");
        header("Content-Length: ".$fileSize);
        header("Content-Disposition: attachment; filename=".$fileName);

        // Output file.
        readfile ($filePath);                   
        exit();
    }
    else {
        die('The provided file path is not valid.');
    }

javascript函数

<script>
function Inint_AJAX() {
try { return new ActiveXObject("Msxml2.XMLHTTP");  } catch(e) {} //IE
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
try { return new XMLHttpRequest();          } catch(e) {} //Native Javascript
alert("XMLHttpRequest not supported");
return null;
};
function download(path,val) {
var req = Inint_AJAX();
req.open("GET", path+"download.php?val="+val); //make connection
//req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header
req.send(null); //send value
req.onreadystatechange = function () {
 if (req.readyState==4) {
      if (req.status==200) {

      }
 }
};

}
</script>

下载链接

<a href="javascript:download('http://localhost/project/images/','DMS.doc');">DMS.doc</a>

【问题讨论】:

    标签: php javascript ajax


    【解决方案1】:

    那么,如何使用 PHP 点击文件名来下载文件?

    最简单的方法应该是链接到它。

    <a href="download.php?......" target="_blank">DMS.doc</a>
    

    【讨论】:

    • 我需要更安全的方式,以便无法下载任何其他文档
    • @OM 安全吗?我不明白。直接链接download.php和在JS函数中创建链接一样安全,不是吗?
    • 使用 AJAX 不会使其安全。您需要使用会话或类似方法在服务器上保护它。
    • @Pekka 它起作用了,但给了我在文件中写为“Warning: readfile(DMS.xls) [function. readfile]: 无法打开流: C:\wamp\www\dfms\support-scripts\download.php24上没有这样的文件或目录>
      "
    • @OM 好吧,在这种情况下,您的文件似乎不存在?
    【解决方案2】:

    你不需要 AJAX,你甚至不能用 AJAX 做你想做的事。如果您想混淆文件的 URL,请将您的下载功能替换为:

    function download(path,val) {
        window.location.href = path+"download.php?val="+val;
    };
    

    【讨论】:

    • 绝妙的解决方案!!谢谢!
    【解决方案3】:
         // Fetch the file info.
    
    
    $filePath = $_SERVER['DOCUMENT_ROOT'] . "/dfms/images/docs/".$filename;
    
    
    
    if(file_exists($filePath)) {
        $fileName = basename($filePath);
        $fileSize = filesize($filePath);
    
        // Output headers.
        header("Cache-Control: private");
        header("Content-Type: application/stream");
        header("Content-Length: ".$fileSize);
        header("Content-Disposition: attachment; filename=".$fileName);
    
        // Output file.
        readfile ($filePath);                   
        exit();
    }
    else {
    

    【讨论】:

    • if(file_exists($filePath)) { $fileName = basename($filePath); $fileSize = 文件大小($filePath); // 输出标题。 header("缓存控制:私有"); header("内容类型:应用程序/流"); header("内容长度:".$fileSize); header("内容配置:附件;文件名=".$fileName); // 输出文件。读取文件 ($filePath);出口(); } 其他 {
    猜你喜欢
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    • 2016-03-25
    • 2018-06-25
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多