0

I want to copy image from a url to my server using PHP.
I saw this answer Saving image from PHP URL
and Tried this :

<?php

$url = 'https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xap1/v/t1.0-1/p200x200/548412_147785902099377_260065314_n.jpg?oh=5c97dcd58931398e501666daee4c4ae8&oe=5457AC73&__gda__=1414911437_494ad1af138ee7670f89f4a6ba8b6d06';
$img = 'flower.jpg';
copy($url, $img);

?>

But i got the following error :

failed to open stream: No connection could be made because the target machine actively refused it.

My php version is 5.4 and in my php.ini allow_url_fopen=On
I cant find how to fix this, any clever suggestions?

Community
  • 1
  • 1
jackkorbin
  • 491
  • 7
  • 22
  • Maybe you can use `file_get_contents`? As in the accepted answer of the question you linked to. – CompuChip Jul 26 '14 at 09:16
  • 2
    That server doesn't like you trying to access its files that way. [See this previous question](http://stackoverflow.com/questions/9695224/no-connection-could-be-made-because-the-target-machine-actively-refused-it-127-0) - in the meantime, take off the `s` in `https` and see if it helps. – Deryck Jul 26 '14 at 09:19
  • @CompuChip - That's exactly the same underlying mechanism. – Álvaro González Jul 26 '14 at 09:23
  • @Deryck same Error still persists. – jackkorbin Jul 26 '14 at 09:37

3 Answers3

0

I can grab it with plain curl at the command line:

curl "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xap1/v/t1.0-1/p200x200/548412_147785902099377_260065314_n.jpg?oh=5c97dcd58931398e501666daee4c4ae8&oe=5457AC73&__gda__=1414911437_494ad1af138ee7670f89f4a6ba8b6d06" > bill.jpg

Maybe you can use php's system to do that?

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
0

Use file_put_contents instead of copy.use below code

file_put_contents($img, file_get_contents($url));
Kumaran
  • 3,460
  • 5
  • 32
  • 34
0

How about using curl, you can then write directly to file.

<?php
$url = 'https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xap1/v/t1.0-1/p200x200/548412_147785902099377_260065314_n.jpg?oh=5c97dcd58931398e501666daee4c4ae8&oe=5457AC73&__gda__=1414911437_494ad1af138ee7670f89f4a6ba8b6d06';

$fp = fopen ('./flower.jpg', 'w+');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, 'YourBot.0.1');
curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  false); //required!
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,  false); //required!
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106