This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
In this video, you'll learn two more ways to use PHP to read a file. These functions will allow us to work with an entire file as a single variable.
Download: File Handling Cheat Sheet
Examples for reading a file
#0. Include or Require puts the contents of the specified file directly into the current file as if you had typed it yourself.
include 'myfile.txt';
#1. Open a connection to a file, using fopen(), and work with that file line by line before you close the resource. Functions for reading each line include fgetc, fgets, fgetss, and fread.
if ($fh = fopen('mytext.txt', 'r')) {
while (!feof($fh)) {
$line = fgets($fh);
echo $line;
}
fclose($fh);
}
#2. Read the entire file into a string using file_get_contents
$file = file_get_contents('myfile.txt');
echo $file;
#3. Read the entire file into an array of lines using file.
$file_lines = file('mytext.txt');
foreach ($file_lines as $line) {
echo $line;
}
NOTE: On the Windows platform, be careful to escape any backslashes used in the path to the file, or use forward slashes.
$fh = fopen("c:\\folder\\myfile.txt", "r");
Filesystem Functions
PHP offers many more functions than what we covered here. Examples (* marks functions we've used in this course):
- basename — Returns trailing name component of path
- chmod — Changes file mode
- chown — Changes file owner
- copy — Copies file
- delete — See unlink or unset
- dirname — Returns a parent directory's path
- * fclose — Closes an open file pointer
- * feof — Tests for end-of-file on a file pointer
- fgetc — Gets character from file pointer
- * fgets — Gets line from file pointer *
- fgetss — Gets line from file pointer and strip HTML tags
- file_exists — Checks whether a file or directory exists
- * file_get_contents — Reads entire file into a string
- * file — Reads entire file into an array
- * fopen — Opens file or URL
- fread — Binary-safe file read
- * is_dir — Tells whether the filename is a directory
- is_writable — Tells whether the filename is writable
- mkdir — Makes directory
- pathinfo — Returns information about a file path
- rmdir — Removes directory
- unlink — Deletes a file
For more functions and options, check out the documentation for Directory Functions and File System Functions.
PERMISSIONS
If you are having problems with accessing files, a good place to start looking is at the file permissions. To learn more about file permissions, check out our Console Foundations course, specifically the video on File Permissions
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up