[MUSIC]
Welcome to Your Computer.
I'm Alena and
I'll be your guide as we learn to communicate with your file system.
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.
While this may sound like an all powerful and lofty goal, and it can be.
File handling is also an important part of any application.
You often need to open and process a file for
different tasks such as reading and config.
You may also need to write files such as exporting order details.
In this course, we'll start by exploring the file structure itself
to read what files are where.
Then we'll move on to different ways of reading and writing those files.
In the next section, we'll explore some common data types and
how to parse that data into something we can use in PHP.
One of the biggest issues you'll run into when trying to read or
write files is permissions.
If you're having any access issues reading or
writing files, check the notes associated with this video.
Are you ready to take control?
Then let's get started.
Open the workspace attached to this video.
At this point, you should be familiar with browsing files here on the left,
opening and closing folders and seeing what's there.
What you're seeing is actually a script that's reading the files and
displaying them as it's been instructed.
So let's read and display these files ourselves using PHP.
Create a New File named index.php.
We'll start with the function name scandir.
This scans the directory and returns an array of files and
directories from the given directory.
I want to look in the data directory, so we'll pass data for the directory.
I'll put the results in a variable named $files.
Let's display these files as a list by looping through the array.
For each files as file.
[SOUND] And
then,we echo file.
Let's preview this in the browser to see what we have so far.
The single dot represents the current directory and
the double dots represent the path to go up one level.
DS_Store is a hidden mac file.
We don't want to show any of these or
any other hidden files that start with the dot.
So let's check the first character.
If the first character is not a dot, then we'll display the file.
Let's preview this in the browser again.
Great, only our folders are displayed but I want this to be recursive and
scan any subdirectories as well.
Let's go back to work spaces and turn this into a function.
ReadFolderFiles and we'll pass the directory.
Now, we can call this function.
And parse data.
Now, we're ready to do something different,
if the file is a directory or not.
If is_dir,
the directory plus the file.
Then we're going to echo the file, and call our function again.
We'll pass dir plus the file.
If it's not a directory, we want to add a link to that file.
Let's jump back to the browser and see what we have now.
Great, just what we want, a list of all the folders and
sub-files in the data directory.
We could style these however we wanted and
apply any action we want to control the behavior of the display.
But, let's get into actually reading those files next.