【问题标题】:Downloading docx / doc files via browser in php在 php 中通过浏览器下载 docx / doc 文件
【发布时间】:2016-03-15 06:54:30
【问题描述】:

请我是 php 新手,我无法弄清楚为什么这段代码不起作用。我尝试下载这个 docx 文件,但是它会下载文件,但是当我打开文件时,文件上没有显示任何内容。它是空的。我相信文件已损坏,这是由于代码造成的。代码如下:

<?php
    if(isset($_POST['file_name']))
    {

        $file = $_POST['file_name'];
        echo $file;

        header('Content-Description: File Transfer');
        header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
        header('Content-Disposition: attachment; filename='.$file.'');
        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($file));
        ob_clean();
        flush();
        readfile('resumeFolder/'.$file);
        exit(); 
    }
?>
<form action="force_download.php" method="post" name="downloadform">
  <input name="file_name" value=" Aunt_CC_Letter.docx" type="hidden">
  <input type="submit" value="Download the MP3">
</form>

【问题讨论】:

    标签: php download header http-headers docx


    【解决方案1】:

    $file 当前仅保存文件名。在readfile($file) 中,$file 应该是服务器上存在的文件的完整路径,而不仅仅是名称。它应该位于该路径上。我认为,就您而言,问题在于服务器上不存在该文件。

    【讨论】:

    • 你能把你的目录结构写到文件里吗?
    • 尝试使用 readfile('./resumeFolder/'.$file);如果 resumeFolder 在您的根目录下。
    • 它在根目录下,我仍然尝试过,但它不起作用。你有在某处有效的例子吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多