PHP Tutorial (& MySQL) #39 - File System (part 1)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay then so in the last few videos we've talked about superglobals and sessions and cookies now I want to shift our focus a little bit to a different part of PHP and that concerns communicating with the file system because PHP can do this it can communicate with files on our computer or in a server so you can already see I've stripped back all of the files on the left over here we no longer have those project files instead we just have this sandbox PHP page which is pretty empty and also this readme text file which we're going to read in a second that's just a bunch of quotes so then PHP makes it really simple to interact with the file system and I'm going to split this over to videos in this first video I'm going to show you how we can do different things with this file like really it find out the file size etc and then in the next video I'm gonna show you a slightly better way to interact with files to read and write to them okay so a very quick and simple way to read a file is to use the function read file so I'm going to store this in a variable first of all I'm going to call this quotes and set it equal to read file and then in there we pass an argument which is the file we want to read which is readme.txt okay so that is going to store the contents of this file inside this variable now and if I try to echo this we should see in the browser okay save and I'll refresh over here and now we can see all of this content right here and at the end notice we have this random number well it's not random that is actually the number of bytes in this file so we get the content followed by the number of bytes okay then so that is the quick and easy way to read a file like I said there is a better way to open a file and keep a reference to that file and that way is going to offer us more options than the refile method but we'll do that in the next video okay so sometimes you might try to read a file but that file doesn't exist so for example if I add an e on the end here that file doesn't exist and if i refresh then we get an error so it would be good to check if that file existed before we do something with it so let me comment this out and instead what I'm going to do is first of all make a variable called file and set that equal to readme.txt because we're going to be using this a fair one as we go forward so now instead of typing this out over and over I just reference this file okay so we're going to do this check to see if this file actually exists before we try to read it so we'll do an if statement and inside we use the function file underscore exists and pass in the file that we want to check which is this thing right here readme text so if that exists then that will return true and we can read the file so we can say echo read file and then the file okay so let's do a little comment above that so we know what it is read file and if this does not exist then this function is going to return a false value and we can do the else statement and inside there we'll just say echo file does not exist okay so let me save this now and the file does exist so we should see the contents on the page let me refresh ok cool that works now if we change this to have an extra e save and refresh then we see the file does not exist awesome so what other things can we do with this file well we could also copy the file so let's do a little comment copy file and the way we do that is by using the function copy and pass in the file and we also need to pass in a second argument and that second argument is going to be the file name that we want to copy this to so I'll call this quotes txt and it doesn't matter that this doesn't exist yet because PHP will create it for us so if I save and run this again then we should see now we have a quote txt file as well cool so that's a nice easy way to copy a file we can also find out the absolute or real path of the file so let's do a comment and say absolute path and then echo will use the function real path and then pass in the file like so let's save that refresh and we see now this thing over here okay what I'm gonna do is just append so BR tax to these things so everything goes on to a new line and it looks a bit cleaner so we'll copy this thing here and just paste it onto this over here as well we don't need it on the copy because we're not echoing anything out there all right so the next thing we can do is find out the file size so file size and then will echo this out as well so file size is the function name as well passing the file and we're going to paste this stuff on the end so we get a br tag after it save and refresh over here and now we can see the file size is three to one and that matches with this over here okay so I'll also show you how to rename the file rename file like so and this time we're not going to echo anything out we don't need to we'll just use the function rename we pass in the file we want to rename and then the second parameter is what we want to rename it to so test.txt save that and refresh now pay attention to this readme file over here I'm going to refresh and now it's called test and it still has all the same content cool so they're some different things we can do with files we can read copy find out the path and the size of the file and also rename the file now one more thing I want to show you is how to make a new directory I'm just going to comment all of this stuff out for a second and then use one final function down here make directory and this function is mkdir it stands for make directory then we pass in the name of the directory we want to make I'm going to call this it quotes and then if we save and run the file we should see that folder appear over here and we can't quotes awesome so there's some very simple very basic different functions that we can use to interact with the file system now in the next video I want to look at a better way to open and read files and also how to write two files to using a method called F open
Info
Channel: The Net Ninja
Views: 23,191
Rating: undefined out of 5
Keywords: php, tutorial, php tutorial, php tutorial for beginners, mysql, mysql tutorial, mysql tutorial for beginners, sql, sql tutorial, php for beginners, learn php, php mysql, php and mysql, php file system, php open, php fopen, fopen, fread
Id: yUzcYBuSgc4
Channel Id: undefined
Length: 6min 36sec (396 seconds)
Published: Tue Mar 12 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.