-1

I'm trying to download an external image on my server with the variable that is written in the url, for example:

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

for this I am trying to use the following PHP code:

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...

It's possible to accomplish this and "ignore" the url that comes after script.php/?

Salatiel
  • 75
  • 1
  • 9
  • 1
    The URL you've posted is wrong, should `www.myserver.com/script.php/imageurl="www.otherserver.com/image.png"` become `www.myserver.com/script.php?imageurl="www.otherserver.com/image.png"` –  Jan 03 '19 at 11:53
  • PHP doesn’t know it’s supposed to be a URL unless it starts with `http://`… – deceze Jan 03 '19 at 11:59
  • @Mike Rodham Ok this solves the problem of the error message, but I have not been able to download the image yet... Is the way I'm trying to download the image correct? – Salatiel Jan 03 '19 at 12:03
  • See answer below. –  Jan 03 '19 at 12:08
  • Possible duplicate of this issue: [https://stackoverflow.com/questions/3488425/php-ini-file-get-contents-external-url](https://stackoverflow.com/questions/3488425/php-ini-file-get-contents-external-url) Please refer here for further details: [https://stackoverflow.com/a/3488430/4420781](https://stackoverflow.com/a/3488430/4420781) You need to set `allow_url_fopen` option in `php.ini` file to allow fetching of urls in `file_get_contents` – Lankesh Zade Jan 03 '19 at 12:13

1 Answers1

1

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:

$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:

$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);
  • Ok, but that's not exactly what I'm trying to do. It would work if I use `$url = $_GET['imageurl'];`? – Salatiel Jan 03 '19 at 12:17
  • What do you mean it's not what you're trying to do? I can clearly see above you're trying to get an image from an external URL and saving it locally. –  Jan 03 '19 at 12:19
  • Your answer about downloading the image does not reference the answer about getting the image from the URL. This image.php does not make sense on this question. – Salatiel Jan 03 '19 at 12:23
  • You're still not quite clear, what are you trying to achieve? What do you want to do with the image? You pass a url to the URL param `imageurl` then what do you want to do with it? –  Jan 03 '19 at 12:25
  • At this point I have a variable called $imageurl that contains a link to an image, I just want to download it. ``$url = $_GET['imageurl'];`` Does it have a chance of working? – Salatiel Jan 03 '19 at 12:31
  • Ok, the copy was the only one that worked, but thank you anyway. :) – Salatiel Jan 03 '19 at 12:48
  • No worries, glad we were able to get there and solve the issue together! –  Jan 03 '19 at 13:18