1

我进行了设置,因此如果图像托管在我的网站上,用户可以通过单击按钮下载图像,但它不适用于外部图像。是否可以使用外部图像(使用 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();
}
4

1 回答 1

1

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

如果启用了fopen 包装器,则 URL 可以用作此函数的文件名。有关如何指定文件名的更多详细信息,请参阅fopen() 。请参阅支持的协议和包装器以获取有关各种包装器具有哪些功能的信息的链接、有关其使用的说明以及有关它们可能提供的任何预定义变量的信息。

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

(代码不需要更改。)

于 2012-06-07T06:10:49.023 回答