Codecademy Logo

File Handling

File System

A file system is a structure of files and directories where computers store all of their information. PHP applications use it for purposes such as content storage, configuration, and logging.

File Handling

File handling is the process by which applications interact with the file system. This includes operations such as creating, reading, updating, and deleting files.

File Pointers

PHP uses file pointers to interact with the file system. A file pointer is a variable that serves as a reference to a specific file. This is obtained by opening a file.

Opening Files

Files must be opened before they can be used. This operation opens a file pointer, which is used to access the file programmatically.

When opening a file, an access mode must be provided. The mode is a one or two-character string that describes the type of access required for the file.

Closing Files

Files must be closed when they’re not actively in use. This prevents issues like data corruption.

By closing a file, the file pointer is invalidated and can no longer be used. To use the file again, we must re-open the file.

fopen()

The fopen() function opens a file and returns a file pointer. The function takes two arguments: a file name and access mode.

$file = fopen("my-file.txt", "a");

Read Mode (r)

The r access mode opens a file for reading.

$file = fopen("my-file.txt", "r");

Write Mode (w)

The w access mode opens a file for writing. Using a file in w mode erases any existing contents of the file or creates the file if it doesn’t already exist.

$file = fopen("my-file.txt", "w");

Append Mode (a)

The a access mode opens a file for appending. Opening a file in append mode preserves any existing contents of the file or creates the file if it doesn’t already exist.

$file = fopen("my-file.txt", "a");

fwrite()

The fwrite() function writes a string to a file. The function takes two arguments: a file pointer and the string being written to the file.

fwrite($file, "Hello, world!"); // writes "Hello, world!" to $file

fread()

The fread() function reads a specific number of bytes from a file, returning the result as a string. The function takes two arguments: a file pointer and the number of bytes to read.

$content = fread($file, 8); // reads first 8 bytes of $file

filesize()

The filesize() function gets the size of a specific file. The function takes a file name as an argument and returns the size of the file in bytes.

$size = filesize("my-file.txt");

file_exists()

The file_exists() function checks whether a file or directory exists. The function takes a file name as an argument, returning true if the file exists or false otherwise.

file_exists("this-exists.txt"); // true
file_exists("this-does-not.txt"); // false

fclose()

The fclose() function is used to safely close a file pointer.

fclose($file);

file_get_contents()

The file_get_contents() function reads an entire file. This provides a simpler alternative to using fopen(), filesize(), fread(), and fclose() to read a file.

The function takes a file name as an argument and returns a string.

$content = file_get_contents("happy-birthday.txt");

file_put_contents()

The file_put_contents() function writes a string to a file. This provides a simpler alternative to using fopen(), fwrite(), and fclose() to write to a file.

The function requires two arguments: a file name and a string to write to the file. To customize the function’s behavior, an optional third argument called a flag can also be included. For example, the FILE_APPEND flag specifies that the file should be appended instead of overwritten.

file_put_contents("file.txt", "Hello, world!"); // writes "Hello, world!" to file.txt, overwriting existing content
file_put_contents("file.txt", " I love PHP!", FILE_APPEND); // appends " I love PHP!" to file.txt

File Handling Errors

File operations in PHP can fail for reasons such as an incorrect file name or insufficient permissions. Most file operations indicate errors by returning false and printing an error message.

$contents = file_get_contents("my-file.txt");
if ($contents === false) {
// read failed
}

Learn More on Codecademy