A Beginners Introduction To PHP
A Beginners Introduction To PHP
(Page 1 out of 3)
Introduction
Welcome to this beginner's introduction to PHP's file functions. In this article I will take
you through all the inbuilt filesystem functions, and explain them. This article is mainly
geared towards beginners, but it could still be useful to more advanced developers as a
simple refresher.
Let's get started with the most basic thing: reading files.
Reading Files
There are several different ways of reading a file, but before you try to read a file, you
have to make sure the file exists first. To do this, PHP comes with an inbuilt function
called is_readable(), which checks if the file exists and if it's readable by your script. It's
possible that your script can't read the file due to permissions, even though the file does
exist. It's used like this:
<?php
$file = 'G:\projects\my_file.txt';
if (is_readable($file) == false) {
die('It doesn\'t exist or we can\'t read it!');
} else {
echo 'It exists';
}
?>
After you've made sure that the file is readable, you can now read the file data. The
easiest way to do this is with the file_get_contents() function, which requires only one
argument - the file path - and immediately returns all the file data. It's used like so:
<?php
$file = 'G:\projects\phpinfo.php';
if (file_exists($file) == false) {
die('It doesn\'t exist!');
}
// Read file
$data = file_get_contents($file);
echo htmlentities($data);
?>
The only downside with file_get_contents() is that it's only supported by PHP 4.3 and
later. This means you will probably have to account for versions lower than that, which
means you can't use the file_get_contents() function. The next best way is to then use the
file() function, which is supported by PHP 3 and up, and is almost identical to the
file_get_contents function. The only difference is that the file function returns the file
data in an array, with each line as a separate item. If you just want the data as a string
(like the file_get_contents function), you have to use the following code:
<?php
// Read file
$data = implode('', file($file));
?>
This method is probably the best way to read a file, and it's supported by any modern
PHP version. But there is yet another way to do it, by manually opening the file, and then
reading in the data. To open a file, you have to use the fopen() function, which opens a
file, and returns a file resource, which can then be used with the fread() function. When
opening a file, you must specify a mode in which to open the file. If all you want to do is
read the file, you need to specify the 'r' mode, like in the example below:
<?php
// Read file
$f = fopen($file, 'r');
$data = fread($f, filesize($file));
fclose($f);
?>
As you can see in the above example, the fread function also needs to know how much
data you want to read, which you must specify as the second argument. Since we want to
read the whole file, the filesize() function is used, which returns the length of the file.
When you're done with the file, you must close it with the fclose() function.
Now that I've shown you how to read a file, let's take a look at writing files.
Writing Files
Since we're now writing a file, you no longer have to check if the file exists, but you do
have to check whether the file you're writing to is writable, with the is_writable()
function. It's used in the same way as the is_readable function:
<?php
$file = 'G:\projects\test.txt';
if (is_writable($file) == false) {
die('Unable to write to file');
}
?>
If the file is writeable, you can then write your data to the file. The easiest way to do this
is with the file_put_contents() function which can be used to write to a file, and used like
this:
<?php
$file = 'G:\projects\test.txt';
if (is_writable($file) == false) {
die('Unable to write to file');
}
// The data
$data = 'This is my