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
Everything on a computer is stored in some sort of file. Understanding how to read and manipulate those files will provide a very powerful tool for controlling the system.
Download: File Handling Cheat Sheet
Directory Functions
Here is a list of directory functions supported in PHP (* marks functions we've used in this course):
- chdir — Change directory
- chroot — Change the root directory
- closedir — Close directory handle
- dir — Return an instance of the Directory class
- getcwd — Gets the current working directory
- opendir — Open directory handle
- readdir — Read entry from directory handle
- rewinddir — Rewind directory handle
- * scandir — List files and directories inside the specified path *
For other related functions such as dirname(), is_dir(), mkdir(), and rmdir(), see the File System Functions.
Secondary Example Using opendir()
Open a directory and read the files one by one.
if ($dh = opendir('data')) {
echo '<ul>';
while (($file = readdir($dh)) !== false) {
if (substr($file, 0, 1) != '.') {
echo '<li>';
echo $file;
echo '</li>';
}
}
echo '</ul>';
closedir($dh);
}
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