【发布时间】:2016-02-02 08:11:24
【问题描述】:
我有一个文件下载代码using php,我在下载页面的代码如下。
if (file_exists($strDownload)) {
//get the file content
$strFile = file_get_contents($strDownload);
//set the headers to force a download
header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=\"" . str_replace(" ", "_", $arrCheck['file_name']) . "\"");
//echo the file to the user
echo $strFile;
//update the DB to say this file has been downloaded
mysql_query("xxxxxxxx");
exit;
}
函数file_exists() 通过有效检查,而我的$strDownload 变量将类似于位于服务器文件夹中的/home/public_html/uploads/myfile.zip。但是当我尝试下载文件而不是下载时,页面会显示文件的完整加密源。如何让它可下载?
编辑: 作为信息,我自己尝试在 wordpress 系统中使用这段代码,我的文件路径将类似于 http://example.com/wp-content/uploads/2016/02/myfile.zip。同样在上面提到的代码中,我自己检查了上面已经提到的服务器路径的file_exists() 条件,并根据需要返回1。
【问题讨论】:
标签: php download force-download