Menu Close

Articles

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 mister of working with file is actually very easy in PHP. It is all about functions available for doing the hard jobs 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!

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?

Hold on there is more:

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

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:

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.

Opening modes:

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

rOpen a file for read only. File pointer starts at the beginning of the file.
wOpen 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.
aOpen 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.
xCreates 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 we have at least 10 students who pass the rating exam with at least 80%. Otherwise details will be available in the technology class.

Read next: Web forms