0

I am using the function file_get_contents() in the script. But it is given false every time.

$image_url = 'http://www.websitename.com/images/imagename.jpg';
$variable = file_get_contents($image_url);
var_dump($variable);

Its returning false

I also checked in phpinfo() settings of the server. Here allow_url_fopen is already on

I don't know why it is returning false. Please help me to figure out this. Many thanks in advance.

Shashank Sharma
  • 585
  • 3
  • 14
  • 1
    What is the image url ? – Sagar Bahadur Tamang Aug 02 '18 at 05:58
  • image url is an image that generated from php script or any external image like http://www.websitename.com/images/imagename.jpg – Shashank Sharma Aug 02 '18 at 06:01
  • It obviously fails to retrieve the URL? There isn't enough info for us to know why. You need to do a `var_dump($image_url)` and check if it contains what you expect. Is it a local URL or a off-site URL? Does it work if you access the URL through a browser? – M. Eriksson Aug 02 '18 at 06:01
  • @Magnus Eriksson $image_url is an external image url. I have not mentioned in the code of segment just because of some confidential info. – Shashank Sharma Aug 02 '18 at 06:03
  • Does it work on any image at all or is it just this specific image that we can't know the URL of that does not work? – Andreas Aug 02 '18 at 06:07
  • _"just because of some confidential info"_ - Please take a step back and look at your question. All we see is that you try to use `file_get_contents()` using a variable that we have _no idea_ what it actually contains, how it was generated or if it even contains a valid URL at all. You say it's an external image URL. As far as we know, that external site might have restrictions against accessing images like that. We have _no idea_. This question is pretty impossible for anyone to answer. You need to do some proper debugging (check the URL, try and access the URL in other ways etc). – M. Eriksson Aug 02 '18 at 06:07
  • @Andreas: This is not working for all image which i have tried from internal server or external server. – Shashank Sharma Aug 02 '18 at 06:12

3 Answers3

1

in most cases its a problem with the SSL certificate of your server, most effected

you can disable the ssl verification with this hack

$stream = stream_context_create(array( 
'ssl'=>array(     
'verify_peer'=> false,     
'verify_peer_name'=> false, ),
'http' => array(     
  'timeout' => 30     ) )     );

 $image_url = 'http://www.websitename.com/images/imagename.jpg';
 $variable = file_get_contents($image_url,0,$stream);
 var_dump($variable);
nh-labs
  • 641
  • 7
  • 10
0

It's return true here try it.

$image_url = 'https://www.gettyimages.ie/gi-resources/images/Homepage/Hero/UK/CMS_Creative_164657191_Kingfisher.jpg';



    $variable = file_get_contents($image_url);

    var_dump($variable);
Mir Shaon
  • 1
  • 4
-2

Put 'http://' before image url

  • 2
    What if the images are under `https://`? Or what if the URL already includes the protocol? Don't post comments as answers. If you don't have enough rep to comment, work on that first. Answers are reserved for actual _solutions_. Commenting is a privilege and trying to bypass the rep restrictions like this is _not_ a good way to earn rep, rather the opposite. – M. Eriksson Aug 02 '18 at 06:14