File System and IO

Updated onbyAlan Morel
File System and IO

PHP allows you to work with the files and directories in the file system that it's being run on. With its built-in file system functions, it allows you to access, manipulate, open, read, write, append, and close files and folders.

Ain't nobody got time for these files.

Opening a File

The fopen function is what you use to open a file on your file system. You need to open a file before you can perform any work on it. This is the general syntax for this function:

PHP
$file = fopen(path, mode);

This function, if successful, returns to you a File object that you can then use further in your code.

Reading a File

Now that you know how to open a file, let's actually read from it. You read from a file using the fread function. This is the basic syntax for the function:

PHP
fread($file, $length);

Using both functions, here's an example of how to read a text file and output all of its contents on the page:

PHP
<?php $path = "document.txt"; $file = fopen($path, "r"); $size = filesize($path); $contents = fread($file, $size); fclose($file); echo($contents); ?>

The mode in our fopen function, r, tells PHP that we are intending to simply read from the file. Since fread() requires a length to read, we pass in the entire size of the file by calling filesize() with the path to the same file.

Now with our $file object and $size we can pass in both to fread() to get the contents of the file.

Then we close the file using fclose() so that it is freed up and can be used by another process or program, then call echo() on the contents.

Reading a File Line by Line

Instead of reading an entire file, you can also choose to read a file line by line. This is done easily using the fgets function:

PHP
<?php $file = fopen("document.txt", "r"); while (!feof($file)) { $line = fgets($file); echo($line . "<br>"); } fclose($file); ?>

The feof() function returns a boolean indicating if the File object is at the end. That means that the while loop will continue running for as long as there is still more left in the file. Then inside the loop we use fgets() with the File object which returns to us a single line at a time. All we do is simply use echo() on that line.

Reading an Entire File

Alternatively, you can choose to read an entire file without needing to open it using file_get_contents(). When you pass in the path to the file you want to read, it will return back the contents of the file:

PHP
<?php $content = file_get_contents("file.txt"); echo($content); ?>

From there you can choose to output it yourself or do anything you want with it.

Reading and Outputting an Entire File

PHP offers a separate function that both reads and automatically outputs the entire file for you. This function is called readfile() and it takes the path to the file as the parameter:

PHP
<?php readfile("file.txt"); ?>

That single function call will look for file.txt and output it directly.

Reading Entire File into Array

Finally, PHP offers another function to read files, file(), that works similarly to file_get_contents() except that the result is in an array and not the entire file as one giant string. This can be useful as you can then iterate over the array line by line and process the data however you want. Here's an example of using file() and iterating over the result:

PHP
<?php $lines = file("file.txt"); foreach($lines as $line) { echo($line); } ?>

As you can clearly tell, PHP offers a wide range of functions for reading a file and it is up to you to pick the one that makes the most sense for what you're trying to do!

Writing a File

PHP provides a function to write to a new file. That function is fwrite(). Here is an example:

PHP
<?php $file = fopen("new.txt", "w"); fwrite($file, "Here is some text."); fclose($file); ?>

It's that simple. Notice that the second parameter for the fopen function is w. This tells PHP that you're going to be writing to this file. When this script runs, you will have a new new.txt file with the text Here is some text. inside.

Writing to a File Without Opening it

As with reading, there are also alternatives to writing to a file. Namely, you can use file_put_contents() to write a new file or overwrite an existing file with some content:

PHP
<?php file_put_contents("file.txt", "Here is some text."); ?>

That will either create a new file called file.txt with your text, or overwrite it if it already exists.

Appending to a File

Appending to a file is almost exactly like writing to a file. You're only changing a single letter, the mode, from w for writing to a to appending:

PHP
<?php $file = fopen("existing.txt", "a"); fwrite($file, "I want to append this."); fclose($file); ?>

This will open the file existing.txt and append the text I want to append this. to it.

Appending to a File without Opening it

Remember file_put_contents()? Well, you can use it for appending as well as for writing. If you pass the FILE_APPEND flag as the third parameter, it will simply append your text to the file instead of overwriting it.

PHP
<?php file_put_contents("file.txt", "Append this text.", FILE_APPEND); ?>

Renaming a File

Renaming a file is straightforward in PHP. Simply call the rename() function with the current path and new path.

PHP
<?php rename("old.txt", "new.txt"); ?>

Deleting a File

Deleting a file is also similarly straightforward in PHP. The function to delete a file is called unlink() and it requires a path to the file to delete. Here's an example of using it:

PHP
<?php unlink("junk.txt"); ?>

Checking if a File Exists

More often than not you're going to want to check if a file exists before performing any critical operations on them. For that PHP offers you the file_exists() function. Pass in a file you want to check and it will return a boolean indicating if the file exists or not.

PHP
<?php $path = "file.txt"; if (file_exists($path)) { // file exists at that path } else { // file does not exist at that path } ?>

File Modes

In this lesson, you saw the modes r for reading and w for writing a file. However, there are more modes that you can use. Here is a list of them:

  • r: Reading the file.
  • r+: Reading and writing the file start at the beginning.
  • w: Writing the file.
  • w+: Reading and writing the file entirely.
  • a: Reading and appending the file.
  • a+: Reading the file.
  • x: Writing a brand new file.
  • x+: Reading and writing a brand new file.

Further Reading

Next Lesson »
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.