PHP can be used to handle files on the server. You can create, access (or read) and manipulate files dynamically using the PHP file system functions. Since PHP is a server side programming language, it allows you to work with files and directories stored on the web server. To work with a file, first you need to open the file.

1. Create a File

$my_file = 'file.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);

2. Open a File

The PHP fopen() function is used to open a file. It requires two arguments - first the file name and then mode in which to operate.

$my_file = 'file.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);

If an attempt to open a file fails then fopen returns a value of false otherwise it returns a file pointer which is used for further reading or writing to that file. After making a changes to the opened file it is important to close it with the fclose() function.

3. Read a File

Once a file is opened using fopen() function, it can be read with a function called fread(). This function requires two arguments - the file pointer and the length of the file expressed in bytes. The length of the file can be found using the filesize() function which takes the file name as its argument and returns the size of the file expressed in bytes.

$my_file = 'file.txt';
$handle = fopen($my_file, 'r');
$data = fread($handle, filesize($my_file));

4. Write to a File

A new file can be written or text can be appended to an existing file using the PHP fwrite() function. This function requires two arguments - a file pointer and the string of data that is to be written.

$my_file = 'file.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
$data = 'This is the data';
fwrite($handle, $data);

5. Append to a File

$my_file = 'file.txt';
$handle = fopen($my_file, 'a') or die('Cannot open file: '.$my_file);
$data = 'New data line 1';
fwrite($handle, $data);
$new_data = "\n".'New data line 2';
fwrite($handle, $new_data);

6. Close a File

$my_file = 'file.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
//write some data here
fclose($handle);

7. Delete a File

You can delete files or directories using the unlink() function.

$my_file = 'file.txt';
unlink($my_file);

Modes of Files and Purpose

  1. r: Opens the file for reading only; Places the file pointer at the beginning of the file.
  2. r+: Opens the file for reading and writing; Places the file pointer at the beginning of the file.
  3. w: Opens the file for writing only; Places the file pointer at the beginning of the file and truncates the file to zero length. If files does not exist then it attempts to create a file.
  4. w+: Opens the file for reading and writing only; Places the file pointer at the beginning of the file and truncates the file to zero length. If files does not exist then it attempts to create a file.
  5. a: Opens the file for writing only; Places the file pointer at the end of the file. If files does not exist then it attempts to create a file.
  6. a+: Opens the file for reading and writing only; Places the file pointer at the end of the file. If files does not exist then it attempts to create a file.