PHP COTW: create, add, read and remove files

In this weeks PHP code of the week we cover the use of files: how to create, add content, read the file contents and delete them. Before you should consider any of the code below you should always perform checks say to make sure the file exists, is readable/writeable, etc.

Create Files

Firstly we want to create a file with no content which is easily done in PHP using the code below.

[php]$filename = ‘test.txt’;
fopen($filename, ‘a’);[/php]

We specify the filename which is test.txt and we use the fopen (file open) function to use the file. As the test.txt file doesn’t exist in our system, in order to use the file PHP creates the file for us.

The second parameter in fopen is the mode parameter and in our case we are using ‘a’ which means append and it appends any content we would of wrote to the file to the end of the file. You could also use ‘r’, ‘w’ and ‘x’ for the second parameter (more information can be found at the php.net website).

Add Content

We will now add some content into the file as shown below.

[php]$filename = ‘test.txt’;
$content = “Some content here”;

$handle = fopen($filename, ‘a’);
fwrite($handle, $content);
fclose($handle);[/php]

So now we have specified some content to append (write) to our file. We open the file as usual, specify the open file reference to the $handle variable and then we use the function fwrite to append our content in $content to our file ‘test.txt’. After that content has been written we close the file using the fclose function.

Some checks you can add onto the above code are shown below:

[php]$filename = ‘test.txt’;
$content = “Some content here”;

if (is_writable($filename)) {

if ($handle = fopen($filename, ‘a’)) {

if (fwrite($handle, $somecontent)) {
echo “Success”;
fclose($handle);
}
}
}[/php]

Read Files

As for reading the file there are many functions available which include file(), fopen() and file_get_contents(). In our case as we might want to get a certain line of information we can use the file() function as seen below.

[php] $filein = file(‘test.txt’);
echo $filein[0]; // Output the first line of the file
echo $filein[2]; // Output the third line of the file [/php]

So we specify the file we want to read and assign the file to the variable $filein. The file function creates a element in the variable array for each line. So the first element in the array is 0 and that would be line 1 in our file.

Now say we wanted to edit a certain line and replace that line we would have to find the line in the file, replace that line with the new line and use fopen and fwrite to re-write the entire file with the replaced content.

Delete Files

To delete files we can use the unlink function as shown below. You are just required to input the file you want to remove and that file will be removed.

[php]unlink(‘test.txt’);[/php]

That’s the basics of using files in PHP. This concludes our PHP code of the week, you should now know most of the basics and regularly used functions to help you develop or improve your own code. (If you ever want to find out what a certain PHP function does, just look it up on www.php.net)

Leave a Reply