Learn web design and programming with our free video and text tutorials.

PHP File Handling Tutorial

PHP allows you to create and open files in your web server using the fopen() function.

The fopen() function accepts 2 arguments and returns a file handle that can be used to read a file and to write content to a file.

The first argument to the fopen() function is the path to the file that you want to create or open. This argument must be a string and is relative to your server root.

The second argument specifies in which mode the file should be opened, for example, you can use the "r" mode to read the file only and to place the cursor at the start of the file.

$file = fopen("textdocument.txt" , "r") or die ("Can not open the file");

The fclose() function is used to close an open file. Once a file is closed using the fclose() function, you can not read, write or append that file unless it is opened again using the fopen() function.