我有一个使用 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
根据需要返回。