Jamie Balfour

Welcome to my personal website.

Find out more about me, my personal projects, reviews, courses and much more here.

Part 3.8Reading and writing files in PHP

Part 3.8Reading and writing files in PHP

Reading and writing files in PHP is quite an essential part being able to write consise and powerful web applications. This article looks into the different methods of reading and writing files in PHP.

Reading files

PHP has many methods of reading byte streams and text into variables or outputting them to the user.

fread

The fread method reads the contents of a file and returns them.

The fread function is very similar to the C method that performs the same task, it even takes the same read write parameters which are described after.

The core of the fread method of opening files is that the file is opened, read and then closed. This is achieved with fopen, fread and fclose. Using these three different methods is a rather old-fashioned but has it's advantages.

Open with:
fopen
Read with:
fread
Write with:
fwrite
Close with:
fclose

The following code sample demonstrates these three methods for reading a file called myFile.txt:

PHP
<?php
	$contents = ""
	$filename = "myFile.txt";
	$max_bytes = filesize($filename);
	$params = "r";

	$handle = fopen($filename, $params);

	$contents = fread($handle);

	fclose($handle);
?>
		

The $params variable contains a string "r" because the file is being opened for read access only. The following table displays all parameters available:

Mode Description
r Open an existing file for standard reading
r+ Opens a file for standard reading and writing
w Opens an existing file for standard writing
w+ Opens a file for standard reading and writing. Also if the file does not exist, it attempts to create it.
a Opens an existing file for writing with the file pointer at the end of the file.
a+ Opens a file for reading and writing with the file pointer at the end of the file.
x Create and open an existing file for writing. If the file exists already it fails and fopen returns false.
x+ Create and open a file for reading and writing. If the file exists already it fails and fopen returns false.

With the w+ option, there was mention that the file system 'attempts' to create a file. The reason it can only attempt is because in the case where another process on the system is trying to remove the directory that the file was to be written to, the system would fail to generate a file. This is just one example of PHP failing to create a file.

readfile

While the fread method works correctly and is fast enough for most tasks, the second method of doing this is sufficiently faster, according to some.

The readfile method is very similar to the fread method.

PHP
<?php
	$contents = ""
	$filename = "myFile.txt";
	$contents = readfile($filename);
?>
		

file_get_contents

The third method of doing this is using the file_get_contents method.

Just like the previous method, this method works on a file name given as a parameter.

PHP
<?php
	$contents = ""
	$filename = "myFile.txt";
	$contents = file_get_contents($filename);
?>
		

Writing files

As with reading files, there are several options for writing files.

fwrite

The first option is the fwrite C-style method. This is almost identical to the way that fread works:

Open with:
fopen
Read with:
fread
Write with:
fwrite
Close with:
fclose

In an almost identical way to the way that fread works, the following sample demonstrates the fwrite method of writing files:

PHP
<?php
	$contents = ""
	$filename = "myFile.txt";
	$max_bytes = filesize($filename);
	$params = "w+";

	$handle = fopen($filename, $params);

	fwrite($handle, "Hello ");
	fwrite($handle, "World!");

	fclose($handle);
?>
		

Note that the $params variable has the value of 'w+' rather than 'r' because to write it needs to have write capabilities. The w+ will create the file if it does not already exist.

file_put_contents

The file_put_contents method is identical to calling the previous method using fopen, fwrite and fclose, except that it does not permit the PHP script to write multiple times in the one file opening.

For the previously mentioned reason, this method is only ideal for quickly updating a file to save on time, but not for multiple writes, where the file being left open is a good thing.

PHP
<?php
	$contents = "Hello world!"
	$filename = "myFile.txt";
	file_put_contents($filename, $contents);
?>
		

This example puts the contents of the variable $contents into the file myFile.txt.

Feedback 👍
Comments are sent via email to me.