从另一个PHP脚本下载文件

时间:2015-12-27 13:36:36

标签: php file download

大家好我有一个问题:从另一个php文件下载文件。 在我创建的页面中有很多下拉列表,当我选择一个选项时,它必须下载一个特定的文件。一切都在我的本地主机上运行完美,但是当我将相同的文件上传到服务器时,我下载的文件被重命名为downloadFile.php,我尝试从链接(标签),jquery,ajax调用downloadFile.php调用,但他们都重命名该文件。 最后我用过:

<select class="form-control" id="cboLinks" onchange="location = this.options[this.selectedIndex].value;">
    <option name="selectFormat" value="">Select a format</option>
    <option name "Text" value="downloadFile.php?userID='.$row['id'].'&format=txt">Text</option>
    <option name="HTML" value="downloadFile.php?userID='.$row['id'].'&format=html">HTML</option>
</select>

在本地它可以工作,但在服务器上没有,它发生的原因是什么人都知道?

修改 BTW这是我从downloadFile.php下载文件的代码

        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($user.$format));
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($user.$format));
        readfile($user.$format);

1 个答案:

答案 0 :(得分:2)

您需要在headers

中返回正确的downloadFile.php

header

中的readfile之前致电downloadFile.php
header('Content-Disposition: attachment; filename="your_file.name"');
// then only call
readfile($file);

还有许多其他标题可以在这里查看 - http://php.net/manual/en/function.readfile.php