1
00:00:00,000 --> 00:00:04,844
[MUSIC]

2
00:00:04,844 --> 00:00:06,260
Welcome to Your Computer.

3
00:00:06,260 --> 00:00:07,910
I'm Alena and

4
00:00:07,910 --> 00:00:10,840
I'll be your guide as we learn to
communicate with your file system.

5
00:00:11,950 --> 00:00:15,370
Everything on a computer is
stored in some sort of file.

6
00:00:15,370 --> 00:00:16,970
Understanding how to read and

7
00:00:16,970 --> 00:00:21,930
manipulate those files will provide a very
powerful tool for controlling the system.

8
00:00:22,930 --> 00:00:28,010
While this may sound like an all
powerful and lofty goal, and it can be.

9
00:00:28,010 --> 00:00:32,420
File handling is also an important
part of any application.

10
00:00:32,420 --> 00:00:35,120
You often need to open and
process a file for

11
00:00:35,120 --> 00:00:38,580
different tasks such as reading and
config.

12
00:00:38,580 --> 00:00:43,380
You may also need to write files
such as exporting order details.

13
00:00:43,380 --> 00:00:47,800
In this course, we'll start by
exploring the file structure itself

14
00:00:47,800 --> 00:00:50,240
to read what files are where.

15
00:00:50,240 --> 00:00:54,500
Then we'll move on to different ways
of reading and writing those files.

16
00:00:54,500 --> 00:00:58,090
In the next section,
we'll explore some common data types and

17
00:00:58,090 --> 00:01:01,140
how to parse that data into
something we can use in PHP.

18
00:01:02,670 --> 00:01:06,230
One of the biggest issues you'll
run into when trying to read or

19
00:01:06,230 --> 00:01:08,880
write files is permissions.

20
00:01:08,880 --> 00:01:11,690
If you're having any
access issues reading or

21
00:01:11,690 --> 00:01:15,100
writing files, check the notes
associated with this video.

22
00:01:15,100 --> 00:01:17,980
Are you ready to take control?

23
00:01:17,980 --> 00:01:19,130
Then let's get started.

24
00:01:20,350 --> 00:01:22,710
Open the workspace attached to this video.

25
00:01:22,710 --> 00:01:27,080
At this point, you should be familiar
with browsing files here on the left,

26
00:01:27,080 --> 00:01:30,110
opening and closing folders and
seeing what's there.

27
00:01:30,110 --> 00:01:33,760
What you're seeing is actually
a script that's reading the files and

28
00:01:33,760 --> 00:01:36,310
displaying them as it's been instructed.

29
00:01:36,310 --> 00:01:39,610
So let's read and
display these files ourselves using PHP.

30
00:01:40,750 --> 00:01:43,635
Create a New File named index.php.

31
00:01:49,716 --> 00:01:52,180
We'll start with
the function name scandir.

32
00:01:54,380 --> 00:01:57,429
This scans the directory and
returns an array of files and

33
00:01:57,429 --> 00:01:59,600
directories from the given directory.

34
00:02:01,170 --> 00:02:06,040
I want to look in the data directory,
so we'll pass data for the directory.

35
00:02:06,040 --> 00:02:09,940
I'll put the results in
a variable named $files.

36
00:02:09,940 --> 00:02:13,617
Let's display these files as a list
by looping through the array.

37
00:02:29,710 --> 00:02:31,526
For each files as file.

38
00:02:38,058 --> 00:02:41,023
[SOUND] And

39
00:02:41,023 --> 00:02:47,698
then,we echo file.

40
00:02:47,698 --> 00:02:50,570
Let's preview this in the browser
to see what we have so far.

41
00:02:53,280 --> 00:02:56,370
The single dot represents
the current directory and

42
00:02:56,370 --> 00:02:59,840
the double dots represent
the path to go up one level.

43
00:03:00,955 --> 00:03:04,490
DS_Store is a hidden mac file.

44
00:03:04,490 --> 00:03:07,090
We don't want to show any of these or

45
00:03:07,090 --> 00:03:09,830
any other hidden files
that start with the dot.

46
00:03:09,830 --> 00:03:11,440
So let's check the first character.

47
00:03:22,398 --> 00:03:28,536
If the first character is not a dot,
then we'll display the file.

48
00:03:32,747 --> 00:03:34,933
Let's preview this in the browser again.

49
00:03:39,760 --> 00:03:44,646
Great, only our folders are displayed but
I want this to be recursive and

50
00:03:44,646 --> 00:03:47,480
scan any subdirectories as well.

51
00:03:47,480 --> 00:03:50,361
Let's go back to work spaces and
turn this into a function.

52
00:03:57,554 --> 00:04:03,554
ReadFolderFiles and
we'll pass the directory.

53
00:04:20,984 --> 00:04:22,659
Now, we can call this function.

54
00:04:28,735 --> 00:04:29,870
And parse data.

55
00:04:31,860 --> 00:04:33,700
Now, we're ready to do
something different,

56
00:04:33,700 --> 00:04:34,988
if the file is a directory or not.

57
00:04:40,197 --> 00:04:43,277
If is_dir,

58
00:04:43,277 --> 00:04:50,279
the directory plus the file.

59
00:04:55,715 --> 00:05:01,072
Then we're going to echo the file,
and call our function again.

60
00:05:07,023 --> 00:05:11,920
We'll pass dir plus the file.

61
00:05:14,280 --> 00:05:17,521
If it's not a directory,
we want to add a link to that file.

62
00:05:54,689 --> 00:05:57,520
Let's jump back to the browser and
see what we have now.

63
00:06:02,531 --> 00:06:05,967
Great, just what we want,
a list of all the folders and

64
00:06:05,967 --> 00:06:08,730
sub-files in the data directory.

65
00:06:08,730 --> 00:06:10,980
We could style these however we wanted and

66
00:06:10,980 --> 00:06:15,450
apply any action we want to control
the behavior of the display.

67
00:06:15,450 --> 00:06:18,170
But, let's get into actually
reading those files next.

