SyntaxStudy
Sign Up
Home PHP Reference file_get_contents() / file_put_contents()

file_get_contents() / file_put_contents()

function

Read an entire file into a string or write a string to a file in a single call.

Syntax

file_get_contents(string $filename): string|false

Example

php
<?php
$json = file_get_contents('data.json');
$data = json_decode($json, true);

file_put_contents('out.json', json_encode($data, JSON_PRETTY_PRINT));
file_put_contents('log.txt', "Log entry\n", FILE_APPEND);

$lines = file('data.txt', FILE_IGNORE_NEW_LINES);