In this lesson, you will learn how to interact with files using PHP. The database is not the only way to store all information on a web server. Sometimes it can be more efficient to store data on files rather than on the database. Other cases in which you may need to modify files, upload images, or log files that you need to process.

File handling methods in PHP

Tip: Operating systems like Windows and Mac OS are not case-sensitive, but Linux and Unix are. It is always a good idea to assume that the system is case-sensitive and stick to lowercase filenames.

Check whether a file exists

PHP lets you determine whether a file already exists or not. You can use the file_exists function which returns either true or false.

Example of checking file existance in PHP

<?php

if( file_exists(“myfile.txt”))

echo “Files exists”;

else

echo “File doesn’t exist”;

?>

Create a file in PHP

If a file doesn’t exist, you need to create it first. fopen() is a PHP function which lets you open a file if its exist, or create one if it doesn’t.

After you execute this code, a new text file will be created in the same directory the PHP file exists. If you receive an error message, your hard disk may be full or, more likely, you may not have permission to create or write to the file.

Example of Creating a File in PHP

$file_handle = fopen("myfile.txt", 'w') or die("Could not create file");

In the above example, the first step is to create a file using fopen() file with ‘w’ mode. Once the file is created, a file handle will be returned. You can now write or read this using fwrite() or fread() functions. After processing the file, you should clean up by closing the file.

Every open file requires a file resource so that PHP can access and manage it. The file hand is a number PHP uses to refer to internal information about the file. Upon failure, false will be returned by fopen().

Upon failure, die() function will print an appropriate function and end the program. This is for the testing purpose only, as in real cases, die() isn’t a preferred way to handle error messages.

PHP File Handling

File modes

While creating a file, you should give a file mode as the second parameter in the fopen() function. File modes can be specified as one of the following options.

  • “r” Opens the file for reading purpose only. It places the file pointer at the beginning of the file.
  • “r+” Opens the file for reading and writing. It places the file pointer at the start of the file.
  • “w” opens the file for writing only. If the file already has content, it will be deleted. It places the file pointer at the beginning of the file and truncates the file length to zero. It will create a file if the file doesn’t exist already.
  • “a” – opens the file for appending the text to the file’s existing content. Open the file for writing only. It open and ape ends to the end of the files. “a+” opens the file for reading and writing. It places the file pointer at the end of the file which helps to append the new text to file’s existing content.

Read a file in PHP

The easiest way to read from a text file is to read the file line by line through fgets().

Example I of Reading a File in PHP

Below is an example of how to read a file using PHP:

<?php

$file_handle = fopen("myfile.txt", 'r') or die("Could not create file");

echo fgets($file_handle);

fclose($file_handle);

?>

Or you can use the fread() to read the whole or partial content of the file being opened.

Here are simple steps required to read a file with PHP.

  1. Open a file using fopen() function
  2. Get the file’s length using filesize() function, in case you want to read the whole file
  3. Read the file’s content using fread() function
  4. Close the file with fclose() function

Example II of Reading a File in PHP

<?php

$file_name ="myfile.txt";

$file_handle = fopen($file_name, 'r') or die("Could not create file");

$file_size = filesize($file_name);

$file = fread($file_handle,$file_size);

fclose($file_handle);

echo $file;

?>

filesize($file_name) would return the length of the file.

fread() function takes two parameters. The first one is the handle of the file being opened. The second is the number of characters you want to read from the file. The following code will read first 10 characters from the file.

fread($file_handle, 10);

Write to a File Using PHP

Writing on a file is really simple in PHP. The steps are as following:

  1. Open a file using fopen() function with w or a file mode.
  2. Write something on the file using fwrite() function
  3. Close the file using fclose() function

Example of Writing a File in PHP

<?php
$file_handle = fopen("myfile.txt", "w");
fwrite($file_handle, "this is text to be written");
fclose($file_handle);
?>
PHP GET and POST Methods Tutorial Home PHP Functions
Last modified: April 12, 2019