PHP File handling Tutorial
The kinds of things that you will do with files include:
• Reading and writing text in files.
• Creating and deleting directories and file manipulation.
Part 1 – File permissions on the server
If you have to write data to a text file on a web server, or you have to copy, rename, delete or do any kind of file manipulation, the file or folder will require correct permissions.
To change file or folder permissions in Dreamweaver, open the site up in the remote view and right click on the file or folder. Select permissions and then choose the kind of permissions that you want. This would be 666 if you want to write and manipulate files.
This may not be necessary, as it may already be set on your web server.
Part 2 – Working with files
The typical way in which you will work with a text file is something like:
• Open the file that you are going to work with by associating a file handle
• Read or write to the file using the file handle
• Close the file using the file handle.
Task 1 – Opening a file
(1) In your text editor create a new PHP web page called file.php. If you are using Dreamweaver, remove all the html code from the page.
(2) Enter the following:
<?php
//file.php
$filename = “data.txt”;
if ( !$fp = fopen($filename, “r”) )
{
die (“cannot open file $filename”);
}
fclose($fp);
?>
The $fp is the file pointer which is used for reading and writing operations.
(3) Upload the file to your web space and run it in your web browser and see what happens.
(4) Create a text file called data.txt and upload it to the web space and run file.php in your browser again.
The fopen() function
fopen() takes two arguments. The first argument is the file name. The file name can be relative to the location of this PHP file, or absolute with the full path name of the file in the form “/myfiles/www/data.txt” or it can be a url or an ftp path name to a different server.
The second argument is the mode which can be:
r Read only access
r+ Read and write access
w Write access only
w+ Read and write access, any existing data will be lost if the file does not exit and PHP will attempt to create the file
a Appending only. Write to the end of the file, if the file does not exist then PHP will attempt to create the file
a+ Open for reading and appending. Data is written to the end of the file, if the file does not exist, then PHP will attempt to create it.
Task 2 – Writing to the file
(1) In file.php, change the mode from r to w+
(2) Before the fclose() line enter the following:
(3) fwrite($fp, “this is some text);
(4) Your file will now look like:
<?php
//file.php
$filename = “data.txt”;
if ( !$fp = fopen($filename, “w+”) )
{
die (“cannot open file $data.txt”);
}
fwrite($fp, “this is some text”);
fclose($fp);
?>
(5) Upload the file to your web server, and run file.php in your web browser. When you examine the data.txt file you should see the extra text.
Task 3 – Reading from the file
There are a number of ways of reading the contents of file. One method is to read a block of characters into an array until the end of the file is reached
while ( !feof($fp) ) {
$lines[] = fgets($fp, 1024);
}
fgets($fp, 1024) means to read a line with up to 1024 bytes or until an end of line is found.
(1) Modify file.php with the above code added and an extra bit of code to display the text in the array.
The file.php code will now look like:
<?php
//file.php
$filename = “data.txt”;
if ( !$fp = fopen(“data.txt”, “w+”) )
{
die (“cannot open file $data.txt”);
}
fwrite($fp, “this is some text\r\n”);
fwrite($fp, “this is some more text\r\n”);
fwrite($fp, “this is even more text\r\n”);
while ( !feof($fp) ) {
$lines[] = fgets($fp, 1024);
}
fclose($fp);
foreach ($lines as $key => $value) {
echo($value . “<br/>”);
}
?>
(2) Upload the file to the web server and run it in your web browser. It should display the text that has been entered into the file.
Part 3 – File and folder manipulation
There are a number of PHP functions that can be used to work with files and folders.
Some of these functions may need permissions to be modified on the server to allow.
copy() copy a file
rename() rename a file
file_exists() shows if the file exists
basename() returns the base part of a file name