我目前在我制作的Wordpress网站上强制下载文件时有一些问题。在我的本地主机上,它工作得很好,但当我尝试到现场服务器的所有文件下载被打破。例如,图像不显示,视频和音频文件不播放等。
这是我的代码:
$title = $query[0]->post_title;
$mimetype = $query[0]->post_mime_type;
$guid = $query[0]->guid;
$filepath = str_replace(home_url().'/wp-content/uploads/', '', $guid);
$download = ABSPATH.'/wp-content/uploads/' . $filepath;
$filesize = filesize($download);
//Download file
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.$title);
header('Content-Type: '.$mimetype);
header('Content-Transfer-Encoding: '.$mimetype);
header('Content-Length: '.$filesize);
ob_clean();
flush();
readfile($download);
exit();
如果我在Chrome中检查响应,它说"未能加载响应数据"在预览和响应选项卡。
任何想法?
将此添加为注释,但不能。使用ABSPATH会给你带来痛苦。我会回显你的$download路径并确保它是正确的。看这里- ABSPATH还是__FILE__?