0

I have a PHP script running on a web server. There I extract all image links from another website. This works fine.

But I have troubles to save the images to my web server. I have tried everything, for example saving the images with FTP (ftp_put), copy($source, $destination) and so on.

  • Everything works fine if the image URL looks like: HTTPS://a-website.com/image.jpg
  • But it does'n work with these image URLs: HTTP://another-website.com/image.jpg

Edit: some code:

$url_1 = "https://a-website.com/image.jpg";
$url_2 = "http://another-website.com/image.jpg";

copy($url_1, "image_1.jpg"); // This works 
copy($url_2, "image_2.jpg"); // This doesn't work. No error or warning, but the file isn't stored.

// Both of these lines work. Both images will be displayed. 
echo "<img src=\"".$url_1."\" >";
echo "<img src=\"".$url_2."\" >";

What is the reason, that HTTP-URLs can't be processed? Is this a restriction of my webhost? My php script runs on a webspace without HTTPS.

Robert
  • 161
  • 1
  • 3
  • 9
  • 1
    not much we can help with without your code? – Mark Twigg Aug 16 '16 at 11:48
  • "it doesn't work"... ok. – Bert Aug 16 '16 at 11:52
  • are you positive the other site actually uses http? most sites have switched to https dumping the http request with a silent redirect to https –  Aug 16 '16 at 12:00
  • @PHClaus: Some of the links are really HTTP. I have already tried to change the links to HTTPS in my script. – Robert Aug 16 '16 at 12:12
  • this may be obvious, but just to be sure, your script checks the remote protocol first, doesn't it? e.g. if..http.. use http, else.. use https -- as mentoioned before, some code would help –  Aug 16 '16 at 12:25
  • @PHClaus I have added some code. – Robert Aug 16 '16 at 12:30

1 Answers1

0

I have just tried this on my localhost and both images are saved and displayed. Hence, it must be something on your host getting in the way.

$url_1 = "http://phclaus.eu.org/favicon.png";
$url_2 = "https://mozilla.org/media/img/favicon/favicon-196x196.cb79ae3b6daf.png";

copy($url_1, "http.png"); // This works 
copy($url_2, "https.png"); // This works, too

EDIT: The folder where you save the images is writeable, isn't it?

  • Thanks for trying it on your localhost! I gave my folder all permissions (777), but it still doesn't work. It seems my host is blocking something. – Robert Aug 16 '16 at 13:18
  • perhaps you'll find something useful in this related post at http://stackoverflow.com/questions/19177070/copy-image-from-remote-server-over-https?rq=1 or this one here http://stackoverflow.com/questions/38501679/https-image-doesnt-displayed-if-i-force-in-http?rq=1 -- the latter hints at a possible reason. If your own server is on https it will most likely deny mixing in non-secure http content. Have a read and post back. –  Aug 16 '16 at 17:18