【问题标题】:Allow user to download external image using PHP允许用户使用 PHP 下载外部图像
【发布时间】:2012-06-11 04:40:14
【问题描述】:

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

这是我在自己网站上用于图像的。

$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();
}

【问题讨论】:

    标签: php image file download transfer


    【解决方案1】:

    正如您在 http://php.net/manual/en/function.readfile.php 上看到的,您可以使用外部 URL。

    如果启用了fopen wrappers,则可以使用此功能将 URL 用作文件名。请参阅fopen() 了解更多详情 指定文件名。请参阅Supported Protocols and Wrappers 链接到有关各种包装器具有哪些能力的信息, 关于它们的使用说明,以及关于它们的任何预定义变量的信息 可以提供。

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

    (此时代码不需要更改。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-18
      • 1970-01-01
      • 2010-10-15
      • 1970-01-01
      • 1970-01-01
      • 2019-01-02
      • 2012-08-16
      相关资源
      最近更新 更多