PHP
file_get_contents
synchronous
asynchronous
web development

file_get_contents synchronous or asynchronous

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

file_get_contents() is synchronous in normal PHP execution. Whether it reads a local file or fetches a remote URL through a stream wrapper, the current script waits until the operation finishes, fails, or times out.

What "Synchronous" Means Here

In PHP, synchronous means the next line does not run until file_get_contents() returns. That return value is either the full contents as a string or false on failure.

php
1<?php
2$start = microtime(true);
3
4$body = file_get_contents("https://example.com");
5
6$elapsed = microtime(true) - $start;
7echo "Downloaded in {$elapsed} seconds\n";
8echo strlen($body) . "\n";

If the remote server takes three seconds to respond, your PHP process is effectively occupied for those three seconds. The call does not continue in the background while the script does other work.

The Function Reads Everything Before Returning

The name is literal. file_get_contents() reads the entire target into memory and then hands it back. That behavior is convenient for small files and simple HTTP requests, but it reinforces the blocking model.

For a local file, the blocking period is usually short:

php
<?php
$text = file_get_contents(__DIR__ . "/notes.txt");
echo $text;

For a remote URL, the function may block on DNS lookup, TCP connection, TLS negotiation, server processing, and network transfer. Even though the URL wrapper feels high-level, the call still runs synchronously from the perspective of your script.

Use Timeouts for Remote Requests

When file_get_contents() is used against a URL, always define a timeout through a stream context. Otherwise, a slow remote service can stall a request worker much longer than you intended.

php
1<?php
2$context = stream_context_create([
3    "http" => [
4        "method" => "GET",
5        "timeout" => 2,
6        "ignore_errors" => true,
7    ],
8]);
9
10$body = file_get_contents("https://example.com/api/status", false, $context);
11
12if ($body === false) {
13    echo "Request failed or timed out\n";
14}

This does not make the call asynchronous. It only limits how long the synchronous call is allowed to block.

What to Use for Asynchronous Work Instead

If you need concurrency or non-blocking behavior, file_get_contents() is the wrong tool. In PHP, asynchronous patterns are usually built with:

  • 'curl_multi_* for multiple HTTP requests in parallel'
  • event-loop libraries such as ReactPHP or Amp
  • queue workers that move slow I/O off the request path

Here is a minimal curl_multi example for parallel HTTP requests:

php
1<?php
2$mh = curl_multi_init();
3
4$a = curl_init("https://example.com/a");
5$b = curl_init("https://example.com/b");
6
7foreach ([$a, $b] as $ch) {
8    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
9    curl_multi_add_handle($mh, $ch);
10}
11
12do {
13    curl_multi_exec($mh, $running);
14    curl_multi_select($mh);
15} while ($running > 0);
16
17echo curl_multi_getcontent($a);
18echo curl_multi_getcontent($b);

That is the kind of code you reach for when you truly need overlap between I/O operations.

Common Pitfalls

The most common mistake is assuming that a URL-based file_get_contents() call is somehow asynchronous because it uses networking. It is not. The PHP process remains blocked until the function returns.

Another issue is loading large files or responses into memory all at once. Since the function returns a complete string, very large inputs can consume more memory than expected. For streaming behavior, use lower-level file handles or cURL callbacks instead.

Timeouts are also often forgotten. A slow upstream service can make a web request feel randomly hung when the real problem is just a blocking read with no sensible deadline.

Summary

  • 'file_get_contents() is synchronous and blocking in standard PHP execution.'
  • The script waits for the full file or response before moving to the next line.
  • Remote URLs are still handled synchronously unless you switch to another I/O model.
  • Use stream contexts with timeouts for safer blocking requests.
  • For real asynchronous behavior, use tools such as curl_multi, ReactPHP, or Amp.

Course illustration
Course illustration

All Rights Reserved.