我正在我的WordPress网站上制作壁纸页面。我希望图像能够在用户点击它时下载到用户的硬盘上。这就是我到目前为止所做的:
<a href="download.php?file=/path/to/image">
<img src="/path/to/image" />
</a>
我的download.php脚本如下(来源:href image link download on click):
$file = $_GET['file'];
if (headers_sent()) {
die('Headers Sent');
}
if (file_exists($file)) {
// Parse Info / Get Extension
$fsize = filesize($file);
$path_parts = pathinfo($file);
$ext = strtolower($path_parts["extension"]);
// Determine Content Type
switch ($ext) {
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: die('Wrong Extension');
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false); // required for certain browsers
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($file)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $fsize);
ob_clean();
flush();
readfile($file);
} else {
die('File Not Found');
}
但是,当我点击图片时,WordPress意识到没有download.php的模板,并将我重定向到主页。我做错了什么?
更新: 据我所知,这不可能这样做,WordPress中的任何ajax调用必须通过admin-ajax.php
答案 0 :(得分:0)
您可以使用以下代码
<a href="<?php echo home_url() ?>download-image.php?file=/path/to/image">
<img src="/path/to/image" />
</a>