PHP File

Summary: in this tutorial, you will learn how to handle files including opening a file, closing a file, reading from a file and writing to a file and testing for the end-of-file using various handy PHP file functions.

A file is a sequence of bytes stored on the storage media such as hard disk. PHP provides you with many useful functions that allows you to handle files effectively.

Getting file information

File path

Different platforms have different ways to handle the file path. For example, the UNIX and Linux systems use slashes as delimiters in file path as follows:

/www/tmp/test.txt

While Windows systems use backslashes as delimiters:

\www\temp\test.txt

PHP converts the backslashes into slashes automatically so it is safe to use the file path with slashes as delimiters.

Get a file name from a path

In order to get a file name out of a file path, you use the basename() function. It accepts a complete file path as a parameter and returns the file name.

The following example illustrates how to extract the index.php file name out of the file path:

Get file extension

To get file extension only, you use the function pathinfo() function as follows:

Get file size

To get the size of the file, you use the file_size() function. The filesize() function returns the size of a given file in bytes, or false in case an error occurred.

The following example shows you how to get the size of the test.txt file that resides in the same directory as the script file.

File time attributes

PHP provides some useful functions that allow you to retrieve time attributes of a file.

  • fileatime() return the last accessed time of the file.
  • filectime() returns last changed time of the file.
  • filemtime() returns last modified time of the file.

All functions above return an UNIX timestamp  that is an integer value which indicate the number of second between UNIX epoch (midnight Jan-01-1970) and the given time and date.  to get the date and time information from the UNIX timestamp, you use the getdate() function. See the following example:

Opening files

Before doing anything else with a file, you need to open it. To open a  file in PHP , you use the fopen() function. The function opens the file and returns a file handle, which is a pointer that associated with the file. Through the file handle, you can manipulate the content of the file.

The fopen() functions takes two parameters:

  • The first parameter is the name of the file that you want to open
  • The second parameter specifies the mode that indicates how the file is being used e.g. open file for reading, writing or appending.

The following example demonstrates how to open an existing file named test.txt for reading:

The following table illustrates the mode that you can use as values to pass them to the fopen() function:

modeDescription
‘r’Open the file for reading only. The file pointer is placed at the beginning of the file
‘r+’Open the file for both reading and writing. The file pointer is placed at the beginning of the file.
‘w’Open the file for writing only. The file pointer is placed at the beginning of the file. If the file exists, it wipe out the file content, otherwise it tries to create it.
‘w+’Open the file for both reading and writing. The file pointer at the beginning of the file. If the file exists, it wipes out the file;s content, otherwise it tries to create it.
‘a’Open the file for writing only. The file pointer at the end of the file. If the file does not exist, it tries to create it.
‘a+’Open the file for both reading and writing. The file pointer is placed at the end of the file. If the file does not exist, it tries to create it.
‘x’Create and open the file for writing only. The file pointer is placed at the beginning of the file. If the file already exists, the function call will fail and return FALSE. In addition, it generates an error of level E_WARNING. If the file does not exist, it tries to create file.
‘x+’Create and open the file for both reading and writing; otherwise it behaves the same as ‘x’ mode
‘c’Open the file for writing only. The file pointer is placed at the beginning of the file. If the file already exists, it is neither truncated nor the function call is failed, otherwise it is created.
‘c+’Open the file for both reading and writing; otherwise it behaves the same as ‘c’.

Notice that you can append the value b to the mode argument to indicate that the file should be treated as a binary file. For example, rb mode is for opening binary file for reading only.

Closing files

PHP automatically closes all the open file when the script ends. However it is a good practice to close a file whenever you complete working with it. To close a file, you pass the file handle to the fclose() function as follows:

Reading from files

PHP provides some useful functions that allows you to read file’s content:

  • fread() – reads the file’s content
  • fgetc() – reads a single character at a time
  • fgets() – reads a single line at a time
  • file() – reads all file’s content into an array
  • fgetcsv() – reads a line of comma-separated values
  • file_get_contents() – read an entire file into a string
  • readfile() – displays the whole file content, without the need of calling the fopen() function.

The following example illustrates how to read an entire file content to a string:

or just simple as follows:

Writing to files

To write content to a file, you use one of the following functions:

  • fwrite() – writes a string to a file.
  • file_put_contents() –  writes a string to a file without the need of calling the fopen() function.

The following example illustrates how to write a string to file using the fwrite() function:

The file’s content now is “file write demo”.

You can also use the file_put_contents() function to write a string to a file:

Testing for the end-of-file

In order to test for the end-of-file, you use the feof() function. It returns true when the file pointer reaches the end-of-file, otherwise it returns false.

You often use the feof() function with fread() or fgetc() functions when reading a file that you don’t know how big the file is. The following example demonstrates how to use the feof() function. We use the fread() function to read 10 characters at a time.

In this tutorial, we have introduced you some basic file operations including opening a file, closing a file, reading from a file, writing to file and testing for the end-of-file using various PHP file functions.

  • Was this tutorial helpful ?
  • YesNo