0

我正在尝试在我的表单中创建下载链接,以便人们可以下载上传文件夹中的文件。我使用下面的代码

<?php

$path = $_SERVER['DOCUMENT_ROOT']."/content/uploads/"; // change the path to fit your     websites document structure
$fullPath = $path.basename($_REQUEST['download_file']);

if (is_readable ($fullPath)) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
    case "pdf"||"docx"||"doc"||"jpg"||"jpeg":
    header(("Content-type: lappication/pdf")||("Content-type: application/msword")||("Content-type: image/jpeg")); // add here more headers for diff.     extensions
    header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");     // use 'attachment' to force a download
    break;
    default;
    header("Content-type: application/octet-stream");
    header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
}
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
readfile($fullPath);
exit;
} else {
        die("Invalid request");
}

然后我通过将下载链接放在我的表格中进行测试

<h2>Forms Available</h2>

<a href="content/grants/download.php?download_file=some.pdf">Download here</a>
<a href="content/grants/download.php?download_file=">Download here</a>
<a href="content/grants/download.php?download_file=IntroLetter.docx">Download here</a>
<a href="content/grants/download.php?download_file=Conference Grant Application Form.doc">Download here</a>

从那个4下载链接。第一个返回“无效请求”,我知道,因为该文件不存在。第二个链接弹出下载框下载空文件。我敢打赌,因为它真的是空的。对于第三个和第四个链接,该文件确实存在于文件夹中,但它返回

内部服务器错误

服务器遇到内部错误或配置错误,无法完成您的请求。

请联系服务器管理员 webmaster@miitresearch-postgraduates.com 并告知他们错误发生的时间,以及您可能所做的任何可能导致错误的事情。

服务器错误日志中可能提供有关此错误的更多信息。

此外,在尝试使用 ErrorDocument 处理请求时遇到 404 Not Found 错误

可能是什么问题呢?问:)

4

0 回答 0