所以我进行了设置,以便用户可以通过点击按钮下载图像(如果它在我的网站上托管),但它不适用于外部图像。是否可以使用外部图像(使用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();
}
感谢任何帮助,谢谢。
答案 0 :(得分:1)
正如您在http://php.net/manual/en/function.readfile.php上看到的,您可以使用外部网址。
如果已启用fopen wrappers,则可以将URL用作具有此功能的文件名。有关如何操作的详细信息,请参阅fopen() 指定文件名。请参阅Supported Protocols and Wrappers 链接到有关各种包装器具有哪些功能的信息, 关于它们的使用和它们的任何预定义变量的信息的注释 可能会提供。
这是一个PHP设置:http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
(代码不需要改变。)