Does file_get_contents() have a timeout setting?

Does file_get_contents() have a timeout setting?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Does file_get_contents() Have a Timeout Setting?

When processing a series of remote resources in a loop, especially those that may take longer than 15 minutes to complete, the behavior of file_get_contents() is often a point of contention. The core question isn't whether the function has a timeout, but rather how its underlying architecture interacts with PHP's request lifecycle and system-level constraints.

The direct answer is nuanced: file_get_contents() does not possess an intrinsic, configurable timeout parameter within its own signature. You cannot pass a duration to the function to tell it to abort after X seconds. Instead, it relies entirely on the global stream wrapper configuration or the underlying SAPI (Server Application Programming Interface) limits to enforce a cutoff.

The Role of socket_timeout and Stream Contexts

While file_get_contents() lacks a direct timeout argument, you can inject one by creating a custom stream_context. This is achieved using stream_context_create(), where you define a configuration array that includes 'timeout' => $seconds. When this context is passed as the second argument to file_get_contents(), it overrides the default behavior.

Here is how you might implement a specific timeout for a single request:

$context = stream_context_create([
    'http' => [
        'timeout' => 600, // 10 minutes in seconds
    ]
]);

// This will wait up to 100 seconds before failing if the resource isn't ready
$content = file_get_contents('https://example.com/slow-resource', false, $context);

If the remote server takes longer than the specified timeout to respond, file_get_contents() will raise a warning and return false. This allows your loop to catch the failure and handle it gracefully without hanging indefinitely.

The Hidden Limit: max_execution_time

Even if you configure a stream context timeout, there is a critical system-level constraint that often overrides local settings: max_execution_time. By default, this is set to 30 seconds in Apache/Nginx environments unless changed via .htaccess, php.ini, or the CLI.

In your specific scenario, where you have already called set_time_limit(0) to remove the script's execution cap, you might assume unlimited runtime. However, this function only affects the execution time of the PHP script itself, not the network read timeout. If a remote server stops responding after 15 minutes but your stream context timeout was set higher (or unset), file_get_contents() will wait until it hits the max_execution_time limit or the SAPI-specific connection limit defined in default_socket_timeout.

It is important to note that set_time_limit(0) does not extend the duration a single blocking network read can persist if the underlying socket is idle. The stream timeout (socket_timeout) is the mechanism that specifically handles "waiting for data" scenarios.

Handling Long-Running Processes in Loops

Your requirement to ensure the next link isn't called until the prior one finishes implies a synchronous, blocking workflow. If you use file_get_contents() without a stream context timeout on a 15-minute resource, the script will simply block for that entire duration. Once that completes, PHP checks max_execution_time again.

If the total runtime of your loop exceeds the server's max_execution_time (despite set_time_limit(0)), you may encounter issues depending on your hosting environment. Some servers enforce a hard limit based on the web request timeout (e.g., Apache's Timeout directive or Nginx's proxy_read_timeout).

If you are using Laravel, consider that while file_get_contents() works fine in CLI scripts where max_execution_time can be safely set to 0 via php.ini, setting it to 0 in a web request context is generally discouraged due to the risk of locking up the worker process. For production workloads involving long-running data fetches, it is often safer to offload this logic to a queue job (like Laravel Queues) rather than executing it synchronously in a request loop.

Summary of Constraints

To summarize the technical reality:

  1. No built-in timeout: file_get_contents() waits indefinitely by default.
  2. Solution: Use stream_context_create() with 'timeout' to enforce a network-level cutoff.
  3. System Limits: Always respect max_execution_time and SAPI-specific socket timeouts, which act as upper bounds regardless of your stream context settings.

By explicitly setting the stream timeout, you prevent your loop from stalling on unresponsive resources, ensuring that your application moves forward even when a single link exceeds reasonable processing times.

Our network