You may well be familiar with databases such as MySQL and Access which are an ever-increasingly common means of storing data. But data is also stored in files, like Word documents, event logs, spreadsheets, image files, and so on. Databases generally require a special query language to retrieve information, whereas files are ‘flat’ and usually appear as a stream of text. Most often when working with files you’ll read from or write to them. When you want to read the contents of a file you first have to open it, then read as much of the contents as you want, then close the file when you’re finished. When writing to a file, it also needs to be opened (or perhaps created if it doesn’t already exist), then you write your data to it, and close the file when you have finished. In PHP5 there are some built-in wrapper functions that handle the opening and closing automatically for you, but it still happens under the hood. You may also find it useful to find out more about the file by examining its attributes before you start working with it. For example, does the file exist? When was it last updated? When was it created? PHP provides a range of functions which allow you to work with files, and in this article I’ll demonstrate some of them for you.
File Attributes
File attributes are the properties of a file, for example its size, the last time it was accessed, its owner, etc. Let’s look at how you find out more about the files you’re working with.File Size
The functionfilesize()
retrieves the size of the file in bytes.
<?php
$f = "C:\Windows\win.ini";
$size = filesize($f);
echo $f . " is " . $size . " bytes.";
When executed, the example code displays:
C:Windowswin.ini is 510 bytes.The use of a file on a Windows system here highlights an important point; because the backslash has special meaning as an escape character in strings, you’ll need to escape it by adding another backslash. If the file doesn’t exist, the
filesize()
function will return false and emit an E_WARNING
, so it’s better to check first whether the file exists using the function file_exists()
:
<?php
$f = "C:\Windows\win.ini";
if (file_exists($f)) {
$size = filesize($f);
echo $f . " is " . $size . " bytes.";
}
else {
echo $f . " does not exist.";
}
In fact many of the functions presented in this section have the same behavior, i.e., emit warnings. I’ve not included a check with file_exists()
in the rest of my examples for the sake of brevity, but you’ll want to use it when you write your own code.
File History
To determine when a file was last accessed, modified, or changed, you can use the following functions respectively:fileatime()
, filemtime()
, and filectime()
.
<?php
$dateFormat = "D d M Y g:i A";
$atime = fileatime($f);
$mtime = filemtime($f);
$ctime = filectime($f);
echo $f . " was accessed on " . date($dateFormat, $atime) . ".<br>";
echo $f . " was modified on " . date($dateFormat, $mtime) . ".<br>";
echo $f . " was changed on " . date($dateFormat, $ctime) . ".";
The code here retrieves the timestamp of the last access, modify, and change dates and displays them,
C:Windowswin.ini was accessed on Tue 14 Jul 2009 4:34 AM. C:Windowswin.ini was modified on Fri 08 Jul 2011 2:03 PM. C:Windowswin.ini was changed on Tue 14 Jul 2009 4:34 AM.To clarify,
filemtime()
returns the time when the contents of the file was last modified, and filectime()
returns the time when information associated with the file, such as access permissions or file ownership, was changed.
The date()
function was used to format the Unix timestamp returned by the file*time()
functions. Refer to the documentation for the date()
function for more formatting options.
File Permissions
Before working with a file you may want to check whether it is readable or writeable to the process. For this you’ll use the functionsis_readable()
and is_writeable()
:
<?php
echo $f . (is_readable($f) ? " is" : " is not") . " readable.<br>";
echo $f . (is_writable($f) ? " is" : " is not") . " writeable.";
Both functions return a Boolean value whether the operation can be performed on the file. Using the ternary operator you can tailor the display to state whether the file is or is not accessible as appropriate.
C:Windowswin.ini is readable. C:Windowswin.ini is not writeable.
File or Not?
To make absolutely sure that you’re dealing with a file you can use theis_file()
function. is_dir()
is the counterpart to check if it is a directory.
<?php
echo $f . (is_file($f) ? " is" : " is not") . " a file.<br>";
echo $f . (is_dir($f) ? " is" : " is not") . " a directory.";
The example code outputs:
C:Windowswin.ini is a file. C:Windowswin.ini is not a directory.
Reading Files
The previous section showed how you can find out a good deal about the files you’re working with before you start reading or writing to them. Now let’s look at how you can read the contents of a file. The convenience functionfile_get_contents()
will read the entire contents of a file into a variable without the need to open or close the file yourself. This is handy when the file is relatively small, as you wouldn’t want to read in 1GB of data into memory all at once!
<?php
$f = "c:\windows\win.ini";
$f1 = file_get_contents($f);
echo $f1;
For larger files, or just depending on the needs of your script, it’s may be wiser to handle the details yourself. This is because once a file is opened you can seek to a specific offset within it and read as little or as much data as you want at a time.
The function fopen()
is used to open the file:
<?php
$f = "c:\windows\win.ini";
$f1 = fopen($f, "r");
Using the function fopen()
, two arguments are needed – the file I want to open and the mode, which in this case is “r” for read. The function returns a handle or stream to the file, stored in the variable $f1
, which you use in all subsequent commands when working with the file.
The most common mode values are:
fopen()
page.
To read from the opened file one line at a time, use the function fgets()
:
<?php
$f = "c:\windows\win.ini";
$f1 = fopen($f, "r");
do {
echo fgets($f1) . "<br>";
}
while (!feof($f1));
fclose($f1);
Using a do
–while
loop is a good option because you may not know in advance how many lines are in the file. The function feof()
checks whether the file has reached its end – the loop continues until the end of file condition is reached.
To tidy up after I’ve finished reading the file, the function fclose()
is used to close the file.
Writing Files
Two commonly used modes when writing to a file using the functionfwrite()
are “w” and “a” – “w” indicates you want to write to the file but it will remove any of the existing file contents beforehand, while “a” means that you will append any new data to what already exists in the file. You need to be sure you are using the correct option!
In this example I’ll use “a” to append:
<?php
$file = "add_emp.txt";
$f1 = fopen($file, "a");
$output = "banana" . PHP_EOL;
fwrite($f1, $output);
$output = "cheese" . PHP_EOL;
fwrite($f1, $output);
fclose($f1);
First the file name is assigned to a variable, then the file is opened in mode “a” for append. The data to be written is assigned to a variable, $output
, and fwrite()
adds the data to the file. The process is repeated to add another line, then the file is closed using fclose()
. The pre-defined constant PHP_EOL
adds the newline character specific to the platform PHP is running on.
The file contents after executing the above code should look like this:
banana cheeseThe convenience function
file_put_contents()
can also write to a file. It accepts the file name, the data to be written to the file, and the constant FILE_APPEND
if it should append the data (it will overwrite the file’s contents by default).
Here’s the same example as above, but this time using file_put_contents()
:
<?php
$file = "add_emp.txt";
file_put_contents($file, "banana" . PHP_EOL);
file_put_contents($file, "cheese" . PHP_EOL, FILE_APPEND);
Working with CSV Files
CSV stands for comma separated variable and indicates the file contains data delimited with a comma. Each line is a record, and each record is made up of fields, very much like a spreadsheet. In fact, software such as Excel provides the means to save files in this format. Here is an example as displayed in Excel 2007 (which doesn’t show the commas):<?php
$file = "employees.csv";
$f = fopen($file, "r");
Now I need to use a function specifically to read the CSV formatted file, fgetcsv()
; a while loop is used to cater for the fact that the number of records is expected to vary:
<?php
$file = "employees.csv";
$f = fopen($file, "r");
while ($record = fgetcsv($f)) {
foreach($record as $field) {
echo $field . "<br>";
}
}
fclose($f);
This will produce output with a field on each line:
Fred Smith 45 Engineer Susan Brown 33 Programmer Ed Williams 24 Trainee Jo Edwards 29 AnalystLet’s examine the loop. First,
fgetcsv()
has one argument only – the file handle; the comma delimiter is assumed. fgetcsv()
returns data in an array which is then processed using a foreach
loop. When all records have been read the file is closed using fclose()
.
Let’s now look at how you can update or write to a CSV file, this time using the function fputcsv()
. Because I am using the same file that I used above, I will open it in append mode, and the data to be added to the file has been defined in an array.
<?php
$file = "employees.csv";
$f = fopen($file, "a");
$newFields = array(
array('Tom', 'Jones', 36, 'Accountant'),
array('Freda', 'Williams', 45, 'Analyst'),
array('Brenda', 'Collins', 34, 'Engineer'));
foreach($newFields as $fields) {
fputcsv($f, $fields);
}
fclose($f);
The contents of the file as displayed in Excel now look like this:
Summary
This article has given you an introduction into working with files and shown how you can find information about files, read from and write to files. There are many other functions you can use when working with files – this article has covered some of the most commonly used. You’ll find more information on PHP’s file system functions page. Image via Marco Rullkoetter / ShutterstockFrequently Asked Questions (FAQs) about Working with Files in PHP
How Can I Open a File in PHP?
In PHP, the fopen() function is used to open a file. This function requires two arguments: the name of the file you want to open and the mode in which you want to open it. For example, to open a file named “example.txt” in read mode, you would use the following code:$file = fopen("example.txt", "r");
The “r” mode opens the file for reading only. Other modes include “w” for writing, “a” for appending, and “x” for creating and writing to a new file. If the file cannot be opened, fopen() will return FALSE.
How Can I Read from a File in PHP?
PHP provides several functions to read from a file. The fgets() function is used to read a single line from a file, while the fread() function can read a specified number of bytes. Here’s an example of how to use fgets():$file = fopen("example.txt", "r");
if ($file) {
while (($line = fgets($file)) !== false) {
echo $line;
}
fclose($file);
} else {
// error opening the file.
}
In this example, the while loop continues until fgets() returns false, which indicates the end of the file.
How Can I Write to a File in PHP?
To write to a file in PHP, you can use the fwrite() or fputs() function. Both functions require two arguments: a file handle and a string of data to write. Here’s an example:$file = fopen("example.txt", "w");
if ($file) {
fwrite($file, "Hello, World!");
fclose($file);
} else {
// error opening the file.
}
In this example, the string “Hello, World!” is written to the file “example.txt”. If the file does not exist, it will be created.
How Can I Close a File in PHP?
After you’re done working with a file, it’s a good practice to close it using the fclose() function. This function requires one argument: the file handle. Here’s an example:$file = fopen("example.txt", "r");
// do some operations on the file
fclose($file);
Closing a file when you’re done with it helps to prevent data corruption and frees up system resources.
How Can I Check If a File Exists in PHP?
PHP provides the file_exists() function to check if a file exists. This function requires one argument: the name of the file. Here’s an example:if (file_exists("example.txt")) {
echo "The file exists.";
} else {
echo "The file does not exist.";
}
In this example, the script will output “The file exists.” if “example.txt” exists, and “The file does not exist.” otherwise.
How Can I Delete a File in PHP?
To delete a file in PHP, you can use the unlink() function. This function requires one argument: the name of the file. Here’s an example:if (unlink("example.txt")) {
echo "The file has been deleted.";
} else {
echo "The file could not be deleted.";
}
In this example, the script will attempt to delete “example.txt”. If the operation is successful, it will output “The file has been deleted.”; otherwise, it will output “The file could not be deleted.”
How Can I Read and Write to a File at the Same Time in PHP?
To read and write to a file at the same time, you can open the file in “r+” or “w+” mode. The “r+” mode opens the file for reading and writing, and places the file pointer at the beginning of the file. The “w+” mode also opens the file for reading and writing, but it truncates the file to zero length first. If the file does not exist, it will be created. Here’s an example:$file = fopen("example.txt", "r+");
if ($file) {
fwrite($file, "Hello, World!");
rewind($file);
echo fgets($file);
fclose($file);
} else {
// error opening the file.
}
In this example, the string “Hello, World!” is written to the file, then the file pointer is rewound to the beginning of the file, and the first line of the file is output.
How Can I Move the File Pointer in PHP?
PHP provides the fseek() function to move the file pointer to a specified location. This function requires two arguments: a file handle and an offset. The offset is the number of bytes to move the pointer. A positive offset moves the pointer forward, and a negative offset moves it backward. Here’s an example:$file = fopen("example.txt", "r");
if ($file) {
fseek($file, 10);
echo fgets($file);
fclose($file);
} else {
// error opening the file.
}
In this example, the file pointer is moved 10 bytes forward from the beginning of the file, and then the line at the new position is output.
How Can I Get the Size of a File in PHP?
PHP provides the filesize() function to get the size of a file. This function requires one argument: the name of the file. Here’s an example:$size = filesize("example.txt");
echo "The size of the file is " . $size . " bytes.";
In this example, the script will output the size of “example.txt” in bytes.
How Can I Copy a File in PHP?
To copy a file in PHP, you can use the copy() function. This function requires two arguments: the name of the source file and the name of the destination file. Here’s an example:if (copy("source.txt", "destination.txt")) {
echo "The file has been copied.";
} else {
echo "The file could not be copied.";
}
In this example, the script will attempt to copy “source.txt” to “destination.txt”. If the operation is successful, it will output “The file has been copied.”; otherwise, it will output “The file could not be copied.”
Iain Tench has worked in the IT industry for 30 years as a programmer, project manager (Prince2 Practitioner), consultant, and teacher. As a project manager, he specialized in integration projects mainly involving payment systems in the financial sector. He now teaches and has acquired a Masters degree in Internet Systems Development as well as a teaching qualification. Iain's specialized areas of teaching is web technologies, mainly programming in a variety of languages, HTML, CSS and database integration. He's also written a book about Dreamweaver CS3 for Hodder Education.