Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

PHP Dictionary

  1. Home
  2. PHP Dictionary
  3. file_get_contents() / file_put_contents()

file_get_contents() / file_put_contents() Since: PHP 4(2000)

Reads and writes file contents in a single call. You can load text files or write data without manually handling streams.

Syntax

// Reads an entire file as a string.
file_get_contents($filename, $use_include_path, $context, $offset, $length);

// Writes a string to a file.
file_put_contents($filename, $data, $flags, $context);

// Reads an entire file as an array of lines.
file($filename, $flags, $context);

Functions

FunctionDescription
file_get_contents($filename)Reads the entire contents of a file as a string. You can also pass a URL to send an HTTP request. Returns false on failure.
file_put_contents($filename, $data)Writes a string to a file. Creates the file if it does not exist, or overwrites it if it does. Pass FILE_APPEND as the third argument to append instead.
file($filename, $flags)Returns the contents of a file as an array, one element per line. Each element includes the newline character; use the FILE_IGNORE_NEW_LINES flag to strip it.

Return Values

file_get_contents() returns the file contents as a string, or false on failure. file_put_contents() returns the number of bytes written, or false on failure. file() returns an array of lines.

Sample Code

<?php
// Read the entire file as a string.
$content = file_get_contents("/var/www/html/data.txt");
echo $content; // Outputs the raw file contents.

// Error handling when the file cannot be read.
$content = file_get_contents("/var/www/html/not_found.txt");
if ($content === false) {
    echo "Failed to read the file.";
}

// Read a JSON file and decode it into an associative array.
$json = file_get_contents("/var/www/html/config.json");
$config = json_decode($json, true);
echo $config["site_name"]; // Outputs the value for the given JSON key.

// Write a string to a file.
$data = "Log entry: " . date("Y-m-d H:i:s");
file_put_contents("/var/www/html/log.txt", $data);

// Append to a file using the FILE_APPEND flag.
$log = date("Y-m-d H:i:s") . " Access received.\n";
file_put_contents("/var/www/html/access.log", $log, FILE_APPEND);

// Write with an exclusive lock using the LOCK_EX flag.
file_put_contents("/var/www/html/counter.txt", "100", LOCK_EX);

// Use FILE_APPEND and LOCK_EX together.
file_put_contents("/var/www/html/access.log", $log, FILE_APPEND | LOCK_EX);

// Read a file as an array of lines using file().
$lines = file("/var/www/html/data.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
    echo $line . "\n"; // Outputs each non-empty line.
}

// Practical example: process CSV data with file().
$lines = file("/var/www/html/users.csv", FILE_IGNORE_NEW_LINES);
foreach ($lines as $line) {
    $columns = explode(",", $line);
    echo $columns[0] . ": " . $columns[1] . "\n"; // Outputs columns 1 and 2 of each row.
}

Notes

file_get_contents() and file_put_contents() are convenient functions that open, read or write, and close a file in a single call. Compared to stream-based operations using fopen() / fread() / fclose(), the code is much more concise, making these functions the go-to choice when working with an entire file at once.

file_put_contents() overwrites the file contents by default. To append instead, pass FILE_APPEND as the third argument. If multiple processes may write to the file simultaneously, use LOCK_EX to acquire an exclusive lock and prevent data corruption.

file_get_contents() can also send HTTP requests by accepting a URL, but if you need fine-grained control over headers or timeouts for API access, consider using cURL instead. To check whether a file exists before reading it, use file_exists().

If you find any errors or copyright issues, please .