简体   繁体   English

如何使用当前的 php 页面 URL 作为外部图像的参考?

[英]How to use the current php page URL as a reference for an external image?

I'm trying to download an external image on my server with the variable that is written in the url, for example:我正在尝试使用 url 中写入的变量在我的服务器上下载外部图像,例如:

www.myserver.com/script.php/imageurl="https://www.otherserver.com/image.png" www.myserver.com/script.php/imageurl="https://www.otherserver.com/image.png"

for this I am trying to use the following PHP code:为此,我尝试使用以下 PHP 代码:

1: header('Content-type: image/png');
2: $imageurl = $_GET['imageurl'];
3: $remote_image = file_get_contents($imageurl);
4: file_put_contents("/tmp/result.png", $remote_image);

The problem is obvious, the page returns an error because it can not find anything in the path specified in the url...问题很明显,页面返回错误是因为在url指定的路径中找不到任何东西......

It's possible to accomplish this and "ignore" the url that comes after script.php/?可以完成此操作并“忽略”位于 script.php/? 之后的 url。

First change the link from首先将链接从

www.myserver.com/script.php/imageurl="www.otherserver.com/image.png"

TO

www.myserver.com/script.php?imageurl="www.otherserver.com/image.png"

You can download an image like so:您可以像这样下载图像:

SOURCE来源

If you have allow_url_fopen set to true:如果您将allow_url_fopen设置为 true:

$url = 'http://example.com/image.php';
$img = '/my/folder/flower.gif';
file_put_contents($img, file_get_contents($url));

OR或者

copy('http://example.com/image.php', 'local/folder/flower.jpg');

Else use cURL :否则使用cURL

$ch = curl_init('http://example.com/image.php');
$fp = fopen('/my/folder/flower.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM