Sage-Code Laboratory
index<--

PHP File Handling

PHP enable server side file manipulation. That can be useful to store or read application data from files. This gives you power but also is dangerous. You can make mistakes like writing in the wrong file or removing a file having somebody eases data in it.

The big secret working with files on server side is actually very easy using PHP. The secret is about the functions available for doing the hard work wasy for you. After you learn the functions all pieces come into place. You can use control flow statements and functions to read file content or write into files.

Reading a file:

For reading files here are the functions you should use:

readfile() Read a closed file and return content into output stream
fopen() Open an existing file
fread() Read a entire content of a file that was open for reading
fgets() Read one row from a text file that was open for reading
feof() Verify is file is finished when reading the file row by row
fclose() Close a file that was open by fopen()

Example:

This short script read the file and display its content. How cool is this? Only one line of code!

<?php
 echo readfile("test.html");
?>

Serious Example:

In this serious example we read a file that is open. If the file do not open you get an unhandled exception on your hands, but you know how to deal with these don’t you?

<?php
$filename = "test.txt";
$myfile = fopen($filename, "r");
echo fread($myfile,filesize($filename));
fclose($myfile);
?>

Hold on there is more:

This is the truly a serious example. Let’s read a file line by line (row by row).

<?php
$my_file = fopen("test.txt", "r") or die("Unable to open test.txt!");
//file traversal
while(!feof($my_file)) {
 echo fgets($my_file) . "<br>";
}
fclose($my_file);
?>

Writing in a file:

For write a file you can use additional functions:

fopen() Open an existing file
fwrite() Write a text into a file
fclose() Close a file

Example:

<?php
 $myfile = fopen("test.txt", "w")
?>

Output:


PHP Warning: fopen(test.txt): failed to open stream: 
Permission denied in index.php on line 2

Oh yes I forget to tell you about permissions. You are a developer so don’t wary to much if you work in a company, submit a ticket for your IT department to get help. If you are the server admin, give your web server permissions.

<?php
$myfile = fopen("file.txt", "w");
$row = "Helo world\n";
fwrite($myfile, $row);
$row = "PHP is great\n";
fwrite($myfile, $row);
fclose($myfile);
?>

Opening modes:

Function fopen() is using a flag as second parameter that need details:

r Open a file for read only.  File pointer starts at the beginning of the file.
w Open a file for write only.  Erases the contents of the file or creates a new file if it doesn’t exist. File pointer starts at the beginning of the file.
a Open a file for write only.  The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn’t exist.
x Creates a new file for write only.  Returns false and an raise an error if file already exists.
r+ Open a file for read and write.  File pointer starts at the beginning of the file.
w+ Open a file for read and write.  Erases the contents of the file or creates a new file if it doesn’t exist. File pointer starts at the beginning of the file.
a+ Open a file for read and write.  The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn’t exist.
x+ Creates a new file for read and write.  Returns false and an raise error if file already exists.

Note: There is more to talk about files. I will update this article after after I practice myself with files. Until then let's talk about somethng else more intresting:


Read next: Web Forms