Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

DaemonKeen's avatar

File_get_contents - Failed to open stream: HTTP request failed!

Hey, basic php function file_get_content every time times out into a "Failed to open stream: HTTP request failed!".

My setup:

  • file is located on the drive
  • file is accessible via browser
  • I'm using localhost environment with HTTP
  • file name does not contain special characters (http://localhost:8000/images/cross.png)

I've tried:

  • checking php.ini for allow_url_fopen to true
  • adding request header
$opts = array('http'=>array('header' => "User-Agent:MyAgent/1.0\r\n"));
$context = stream_context_create($opts);
$file = file_get_contents(asset('images/cross.png'), false, $context);
  • opening different image on the internet - that WORKS
  • opening different image on disk - doesn't work

Any idea why it's not working? I originally wanted to perform imagecreatefrompng() function and I figured out that basic file_get_contents doesn't work.

0 likes
3 replies
Tray2's avatar

My guess is that you are in the wrong directory . Have you run the php artisan storage:link command?

DaemonKeen's avatar
DaemonKeen
OP
Best Answer
Level 1

I figured it out. You have to use public_path('your/path') not asset('your/path') for file_get_contents() and imagecreatefrompng().

1 like

Please or to participate in this conversation.