Php-我没有得到我的输出下载损坏的文件


Php- I have not got my output for download corrupted files

下载后我无法打开文件。

它说文件已经损坏。

我想我已经很好地使用了所有必需的标题。

在chrome中,它显示如下错误:chrome资源被解释为文档,但使用mime类型的应用程序/八位字节流传输

在Firefox中没有错误消息。

if (isset($_GET['file']) && basename($_GET['file']) == $_GET['file']) {
$filename = $_GET['file'];
} else {
$filename = NULL;
}
$err = 'Sorry, the file you are requesting is unavailable.';
if ($filename)  {
// define the path to your download folder plus assign the file name
$path = '/wp-content/uploads/'. $filename;
// check that file exists and is readable

if (file_exists($path)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="'. basename($path) . '"');
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Pragma: public');
            header('Content-Length: ' . filesize($path));
            ob_end_clean();
            flush();
            readfile($path);
            exit;
        }

    }

下载:从ftp文件夹下载。

没有打开任何格式。

.txt正在被打开。如果我走错了方向,请告诉我。

插入表格:

echo "<tr><a href='?file=". $row["FileupName"]."'>".$row["FileupName"]."</td></tr>";

readfile('$path')是您的问题,它应该是readfile($path)(没有引号)

在PHP中,如果字符串是用双引号"定义的,则仅在字符串中计算变量。实际上,您下载的文件的内容是文本字符串"$path",文件大小不正确。