繁体   English   中英

使用PHP强制下载图像的对话框

[英]Force download dialog for an image with PHP

我在index.php有一个像这样的链接:

<a href="download.php?url=http://example.com/image.jpg">Download</a>

我需要在单击该链接时打开下载Dialong,以使用特定名称(例如myfile123.jpg保存照片。

在我的download.php我有这个:

header('Content-type:image/jpeg');

$handle = fopen($_GET['url'], "rb");
while (!feof($handle)) {
  echo fread($handle, 8192);
}
fclose($handle);

在检索图像时,它只是在同一选项卡中打开它(而不是强制对话框)。

看一下PHP readfile示例

来自php.net的示例:

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.basename($file).'"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($file);

您需要添加另一个header ,以触发下载,例如:

header('Content-Disposition: attachment; filename="image.jpg"');

有关Content-Disposition标头的更多信息,请参见: https : //developer.mozilla.org/es/docs/Web/HTTP/Headers/Content-Disposition

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM