允许用户使用PHP下载外部图像 [英] Allow user to download external image using PHP

查看:69
本文介绍了允许用户使用PHP下载外部图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对其进行了设置,以便用户可以通过单击按钮下载图像(如果该图像托管在我的网站上),但不适用于外部图像。是否可以使用外部图像(使用URL)执行此操作,而无需先将其复制到服务器上的文件夹中?

I set it up so a user can download an image with the click of a button if it's hosted on my site, but it doesn't work for external images. Is it possible to do this with external images (using the URL) without first copying it to a folder on my servers?

这是我自己用于图像的功能

This is what I use for images on my own site.

$str = $_GET['image'];
$img_name = substr(strrchr($str, '/'), 1);
$image = "../u/i/".$img_name;

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


推荐答案

如您在< a href = http://php.net/manual/zh/function.readfile.php rel = nofollow> http://php.net/manual/zh/function.readfile.php 您可以使用外部URL。

As you can see on http://php.net/manual/en/function.readfile.php you can use external URLs.


如果打开包装器已启用。有关如何
指定的详细信息,请参见 fopen()文件名。请参阅受支持的协议和包装程序,以获取有关哪些功能的信息的
链接。各种包装器都有它们的用法,
注释以及它们
可能提供的任何预定义变量的信息。

A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.

这是一个PHP设置: http:// www .php.net / manual / en / filesystem.configuration.php#ini.allow-url-fopen

It's a PHP setting: http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen

(然后无需更改代码)

这篇关于允许用户使用PHP下载外部图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆