Introduction

This tutorial will teach you how to read and write basic text files using PHP. Elementary knowledge of PHP, such as using variables, statements, and functions, is assumed.

Reading A File

To read the contents of a file into a variable, we use the file_get_contents() function. This function accepts a filename as an argument and reads the file into a string variable.

$file_contents = file_get_contents('example.txt');
echo $file_contents;

By default, file_get_contents() will read in the entire file. If you only want to read in part of the file, you can specify offset and maxlen flags as parameters, which will allow you to specify where to start reading and how many characters to read in, respectively.

$start = 10;  // start at the 10th character
$length = 50;  // only read in 50 characters max.
$file_contents = file_get_contents('example.txt', null, null, $start, $length);

The filename can be a local file relative to the current script, such as in the examples above. Additionally, the file can be located on a remote server if your installation of PHP has been configured to allow it:

$file_contents = file_get_contents('https://www.example.com');
echo $file_contents;

How do you know if your file was read successfully? If file_get_contents() fails, it will return a boolean FALSE. Thus, we can check for file read failures:

$file_contents = file_get_contents('example.txt');
if( $file_contents === false ) {
  // file wasn't read successfully, handle the error
}
else {
  echo $file_contents;
}

Writing A File

Writing a file is just as straightforward as reading one. To write to a file, we use the function file_put_contents(). We pass two basic arguments to this function, a filename and the data we wish to write to it. Upon success, the function returns the number of bytes that were written.

If the file does not exist, it will be created.

$data = "Hello World!";
$num_bytes = file_put_contents('example.txt', $data);
echo $num_bytes . ' bytes were written';

By default, file_put_contents() will completely overwrite the file if it already exists. If we wish to append to an existing file, we must specify the FILE_APPEND flag:

$data = "Hello World!";
$num_bytes = file_put_contents('example.txt', $data, FILE_APPEND);
echo $num_bytes . ' bytes were written';

We use the same mechanism to check for errors as we did when reading a file. The file_put_contents() will return boolean FALSE if it is unable to successfully write to the file:

$data = "Hello World!";
$num_bytes = file_put_contents('example.txt', $data);
if( $num_bytes === false ) {
  // file wasn't written successfully
  echo 'Error writing file';
}
else {
  echo $num_bytes . ' bytes were written';
}

Conclusion

You should now have a basic understanding of how to read and write simple text files in PHP.

If you have any questions regarding this tutorial, feel free to reply. Be sure to read the documentation for the two PHP functions used in this tutorial if you get stuck:

https://www.php.net/function.file-get-contents
https://www.php.net/function.file-put-contents

This page was published on It was last revised on

Contributing Authors

0

0 Comments

  • Votes
  • Oldest
  • Latest