简体   繁体   English

如何允许用户下载图片(.gif,.png,.jpeg)?

[英]How can I allow users to download pictures (.gif,.png,.jpeg)?

How can I allow users to download pictures saved on the server? 如何允许用户下载保存在服务器上的图片? The goal is to have the user click on a link and have a specified image to start to download. 目的是让用户单击链接并具有指定的图像以开始下载。

Facebook example: Facebook的例子: 图片下载

Make the link to another .php page, not the image. 链接到另一个.php页面,而不是图像。 Then on that page use the content-disposition header like this: 然后在该页面上使用content-disposition标头,如下所示:

<?php
// Define the name of image after downloaded  
header('Content-Disposition: attachment; filename="file.jpg"');  

// Read the original image file  
readfile('file.jpg');  
?>  

From there, you can just add the filename of the image in a get command like 从那里,您可以仅在get命令中添加图像的文件名,例如

`download.php?filename=file` 

then reference that in the file as: 然后在文件中引用为:

readfile($_GET['filename'].'.jpg')

You need to set a specific header on the response that delivers the image in order to force a download. 您需要在传递图像的响应上设置特定的标头,以强制下载。

Content-Disposition: attachment; filename=myawesomefilename.png

Otherwise it will just load up in browser. 否则,它将仅在浏览器中加载。

So send that header and then just link to the path that delivers that image with that header. 因此,发送该标头,然后仅链接到使用该标头提供该图像的路径。

Send a header to tell the browser to download it like this: 发送标头,告诉浏览器按如下方式下载它:

header("Content-type: application/force-download")

Then send them the data for the file itself without any HTML or anything. 然后向他们发送文件本身的数据,不带任何HTML或任何内容。

This example is snipped from the PHP docs 此示例摘自PHP文档

<?php
$file = 'monkey.gif';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>

If by "download pictures saved on the server" you mean "try to make the browser offer a 'save as' dialog box instead of just displaying the image" then you might want to look into using the Content-Disposition: attachment header in the response that serves the image: 如果通过“下载保存在服务器上的图片”的意思是“尝试使浏览器提供“另存为”对话框,而不是仅显示图像”,那么您可能希望使用“ Content-Disposition:”附件标头提供图片的响应:

Content-Disposition: attachment; filename="thefilename.jpg"

You can set headers in php using the header function . 您可以使用header函数在php中设置标题。

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

相关问题 PHP:如何创建(PNG,JPEG或GIF)图像并直接在base 64中编码而不保存图像? - PHP: How can I create a (PNG, JPEG or GIF) image and encode it directly in base 64 without saving it? PHP GD Library中没有PNG支持,但支持GIF和JPEG。我怎样才能解决这个问题? - No PNG support in PHP GD Library but has GIF and JPEG Support. How can I fix this? 如何使用PHP将.gif,.jpg或.jpeg转换为.png? - How do I convert a .gif, .jpg or .jpeg to a .png using PHP? 允许用户将PHP页面下载为JPEG - Allow Users Download a PHP page as JPEG 我如何只允许特权用户使用php下载pdf? - How can I allow only privledged users to download a pdf with php? 如何允许用户将Facebook中的现有图片添加到我的网站? - How can I allow users to add existing pictures from Facebook to my site? 如何以编程方式检查图像(PNG,JPEG或GIF)是否已损坏? - How do I programmatically check whether an image (PNG, JPEG, or GIF) is corrupted? 如何允许使用 php 上传透明的 gif 或 png - How to allow upload transparent gif or png with php 如何在PHP中将图像(GIF,JPG,JPEG,PNG)转换为TIFF? - How to convert images (GIF, JPG, JPEG, PNG) to TIFF in PHP? 我如何允许用户使用 php 中的 HTACCESS 从特定网页访问下载链接 - How i can allow users to access download link from the specific web-page with HTACCESS in php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM