Reading 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.
PHP has many methods of reading byte streams into a variable.
fread method reads the contents of a file
and returns them.
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
method of opening files is that the file is opened, read and then closed. This is
Using these three different methods is a rather old-fashioned but has it's advantages.
The following code sample demonstrates these three methods for reading a file called myFile.txt:
<?php $contents = "" $filename = "myFile.txt"; $max_bytes = filesize($filename); $params = "r"; $handle = fopen($filename, $params); $contents = fread($handle); fclose($handle); ?>
variable contains a string
because the file is being opened for read access only. The following
table displays all parameters available:
|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
|x+||Create and open a file for reading and writing. If the file exists already it fails and
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.
fread method works correctly and is fast enough for most tasks, the second method of doing this is
according to some.
method is very similar to the
<?php $contents = "" $filename = "myFile.txt"; $contents = readfile($filename); ?>
The third method of doing this is using the
Just like the previous method, this method works on a file name given as a parameter.
<?php $contents = "" $filename = "myFile.txt"; $contents = file_get_contents($filename); ?>
As with reading files, there are several options for writing files.
The first option is the
C-style method. This is almost
identical to the way that
In an almost identical way to the way that
works, the following sample demonstrates
method of writing files:
<?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
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 method is identical to calling the previous method using
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 $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.