大家好,我有一个问题:从另一个php文件下载一个文件。在我创建的页面中有很多下拉列表,当我选择一个选项时,它必须下载一个特定的文件。在我的localhost中,一切都很完美,但当我将相同的文件上传到服务器时,我下载的文件被重命名为downloadFile.php,我尝试过从链接(一个标签)、jquery和ajax调用调用downloadFilephp,但它们都重命名了文件。我上一次使用:
<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>
在本地它可以工作,但在服务器上它不工作,有人知道发生这种情况的原因是什么吗?
编辑:顺便说一句,这是我从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);
您需要在downloadFile.php
中返回正确的headers
在downloadFile.php
中,在readfile
之前调用header
header('Content-Disposition: attachment; filename="your_file.name"');
// then only call
readfile($file);
还有许多其他标头,您可以在此处使用check-http://php.net/manual/en/function.readfile.php