Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9293523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:09:33+00:00 2026-06-18T21:09:33+00:00

When I am using a file_get_content to download result of php script execution: //mainfile.php

  • 0

When I am using a file_get_content to download result of php script execution:

    //mainfile.php
    $ctx = stream_context_create(array(
        'http' => array(
            'timeout' => $this->cfg['gp_timeout']/2
            )
        )
    );
    $url_xml='http://test.server.eu/webgatescript.php?getphoto&param1=1&test=1&param2=2&param3=3';
    $webgateResponse=file_get_contents($url_xml,false,$ctx);

As I know, function file_get_content returns string – content of file, if file is not exist returns FALSE.

From time to time function file_get_content returns FALSE, despite I am in 100% sure that remote file returned content (xml file in my case, I have access and logs). Furthermore file_get_content returns FALSE few microsecond after end of execution remote script, but it takes less time then timeout was setted.

Part of logs:

10:22:04<?xml version="1.0" encoding="UTF-8"?>
10:22:16<response><htlName/><lang/><texts/></response>
10:22:46<?xml version="1.0" encoding="UTF-8"?>
10:22:58<response><htlName/><lang/><texts/></response>
10:23:28<?xml version="1.0" encoding="UTF-8"?>
10:23:29<response><htlName/><lang/><texts/></response>
10:23:59<?xml version="1.0" encoding="UTF-8"?>
10:23:59<response><htlName/><lang/><texts/></response>
10:24:29<?xml version="1.0" encoding="UTF-8"?>
10:24:29<response><htlName/><lang/><texts/></response>

Warning: file_get_contents(http://test.server.eu/webgatescript.php?getphoto&param1=1&test=1&param2=2&param3=3): failed to open stream: HTTP request failed!  in /home/www/mainfile.php on line 22

Call Stack:
    0.0002      93616   1. {main}() /home/www/mainfile.php:0
      175.5346     109072   2. file_get_contents(string(172), bool, resource(14) of type     (stream-context)) /home/www/mainfile.php:22

10:24:5910:25:46<?xml version="1.0" encoding="UTF-8"?>
10:25:47<response><htlName/><lang/><texts/></response>

I made some tests, one of them was running file mainfile.php in exec (I have both files on this same test server, but on production server that will be two separated systems)

    exec("php webgatescript.php >> tom2.log");

and everyone case of running webgatescript.php is logging.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T21:09:35+00:00Added an answer on June 18, 2026 at 9:09 pm

    I wouldn’t rely on file_get_contents.

    A lot of (shared) webservers don’t allow file_get_contents to fetch files from other servers, but do allow the use of cURL.

    $url = 'http://www.stackoverflow.com';
    
    // start cURL
    $ch = curl_init($url);
    
    // set options (http://www.php.net/manual/en/function.curl-setopt.php)
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4); // four seconds until connect timeout
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // we want the response in a variable
    curl_setopt($ch, CURLOPT_TIMEOUT, 4); // four seconds until timeout
    
    $response = curl_exec($ch); // get contents
    
    curl_close($ch); // close the cURL handle
    

    It’s not easy, but is very reliable.


    I (probably) found the answer! As I mentioned, you can set an option that allows or disallows file_get_contents (and some other functions) to get files from an URL.

    I have found the Filesystem and Streams Configuration Options. The allow_url_fopen setting can be used to allow or disallow opening URL’s using fopen, which applies to file_get_contents too. It should be enabled on default, so I don’t know why it doesn’t work for you.

    Either way, I’m still pro-cURL 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I start a download using PHP with this code: <? $_REQUEST['file'] = urldecode($_GET['file']); header(Content-type:
I'm using an AsyncTask class to download data from a php file. After downloading,
I tried to download files in a htaccess protected directory using php and curl.
I am using a solution from this SO question to make a user download
Is this possible using php + ffmpeg? ffmpeg-php has the ability to: Ability to
Currently I'm using file_get_contents() to submit GET data to an array of sites, but
I'm using PHP's function file_get_contents() to fetch contents of a URL and then I
I'm using python to programatically download a zip file from a web server. Using
I've a php code for a file download for specific user I'm storing the
Im using the following code to present a file for download. The file saves

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.