PHP Front To Back [Part 19] - File System Functions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys in this episode of PHP front-to-back we're going to be working with files and folders and file paths alright this is actually a very powerful part of PHP is is working with the file system and being able to write to files and create them and read from them and so on so I've created a new folder called website 7 in the PHP sandbox and I just have an empty index.php file alright so in here I'm going to just put in my PHP tags and first thing we're going to do is look at a couple functions that work with file file paths so let's say we want to return the file name with the file extension so I'm just going to create a variable here with a path to a file so we'll say slash there are one slash we'll just call this my file dot PHP all right so let's say we want to get the file name so we could use a function called base name and I'm going to just comment this file really good so you guys have this as a reference so this is going to return file name so we'll say echo base name and we'll pass in the path ok we'll open it up over here local host PHP sandbox web site seven and we get my file dot PHP ok so it looked at this it knows that this is the path it knows that this is the file name alright now if we wanted to get just the file name without the PHP extension we can do that as well we'll say return filename without extension so for that we can just we'll just say echo base name path and then we'll just add another parameter and say dot PHP so let's just comment this out here and save that reload and we just get my file ok so that's how you can get it without the file extension all right so the next function we're going to look at is their name and what that will do is it will return the directory name from the path so let's say echo der name and let's just put something in here actually we'll just put that file I mean that path variable okay it's reload and we get slash door 1 so it looks at this it knows again this is the file name but it gives us the path ok as opposed to base name which gave us the file so even if we were to put more than one level here if we were to say do that and reload it will give us 0 dyrone alright so what I want to do now is I want to create a file so let's go in website seven I'm going to create a text file and from here let's just say hello world and we'll save this as file 1 dot txt I don't know why it takes a couple seconds to create that it's weird okay so let's let's say we want to check to see if this if that file actually exists so there's a function called file underscore exist that we can use so file underscore exists and we're going to pass in here file 1 dot txt actually know what I'll do is to put variable up here and set that to file 1 dot txt and then I'll just put that in here all right so let's go ahead and reload that and we get a 1 it's looking in the current directory for file 1 if I were to change this to file 2 and reload we're going to get false or nothing ok so you could put this in an if statement and check for it as well okay so that's file exists just going to continue to comment this stuff out and let's let's look at real path okay so what what real path will do is it gets the absolute path so the it gets the whole thing not just the relative path so let's say echo real path and we'll pass in file and reload and we get nothing that's because there is no file - so let's change that back to file 1 and now you can see it gets from the absolute route I'm on Windows so it's looking in the C Drive into xampp htdocs PHP sandbox website 7 and file 1 dot txt alright now we have another function called is file and what that does is it checks to see if file ok so let's do echo is file file ok so let's see what that gives us that gives us a 1 just like file exists the difference between these is that we can use file exists for a folder as well so if we create a new folder we'll just call it test and we run out say is file test and reload we get nothing we get false if we do file exists oops file exists and reload we get one okay because it will return one even if it's a folder is file will not that's the difference all right so another thing we can do is we can check if a file is writable okay so let's do echo is underscore writable and we'll pass in our file variable which is file 1 dot txt and reload and we get a 1 now if I go to my operating system and go to the file 1 dot txt and I go to properties and then set it to read-only and go back and reload we get false because now it's not writable ok let me just change that back and in addition to that you can also do is readable ok so if you had some kind of permissions on that made the file not readable that's going to return it's going to return false another thing we can do is get the file size so if we say echo and as a function called file size and we'll go and reload and we get 11 and if we go to that file and let's look at the properties here you'll see that the size is 11 bytes ok so what I want to do now is give you some functions that can manipulate the file system we can create directories and files and copy them rename them do different things like that so first one we'll take a look at will be to create a directory or folder and that's going to be MK dir just as you would do in the terminal alright so let's say make directory and let's see I'm going to delete that test direct folder that we created and let's make one called testing okay so if we save that and we go and reload and now see over here a testing folder was just created so that's that pretty easy another thing that we can do is we can delete a directory so the the rmdir command will delete a directory as long as there's no files in it so let's say rmdir testing and let's reload and you'll see that that is now deleted all right so this will remove directory if not empty or if empty sorry oops okay let's see what else can we do here let's uh I don't want to get into creating a file just yet let's copy a file so let's say we want to take file1.txt which has hello world in it and we want to copy it to another file or basically take it and create another file with it with the same content so to do that we can use the copy command so let's do echo copy and we'll take this takes two parameters first one is going to be the file we want to copy second one is going to be the file we want to create from it so we're going to take file 1 and we're going to create file 2 with the same content so let's reload and now you can see it created file 2 text which has hello world in it all right we can also rename a file so let's say rename and we'll take file 2 dot txt and let's rename it to my file dot txt ok we'll reload and now you can see file 2 is changed to my file dot txt still as HelloWorld in it okay say rename file okay we can also delete a file and we can do that with the unlink command so let's say unlink and we'll pass in here my file dot txt go ahead and reload and now my file txt is gone so you can see we can do pretty much anything in terms of creating and copying and all that stuff now let's say we want to read what's in the file and output it on to the screen in our browser so it's a write write from file to string so for that we can use something called file get contents so let's do echo file underscore get underscore contents and then the file name I'm just going to put in the variable we have which is file 1 dot txt and we'll save that and reload and we get hello world ok so this will take whatever file we pass in and just echo it out into the browser now we can also write a string to the file so let's do that so for that we can use file put contents ok it's going to take the file name and then it's going to take what we want to pass in so let's say goodbye world okay I'll run that and it that what it passes us back if we echo this out is I believe that's the file size let me just check that yeah that's the file size but if we look in file 1 now you'll see goodbye world now notice it didn't just add this to the file it replaced what was there which was hello world now if we wanted to append to this instead of replace it we have to do something a little different so I'm going to go ahead and just put hello world back and then what we'll do is first we'll get it and then we'll append onto it and then put it back so let's say we'll say current and set that to file get contents okay and then what we'll do is take that current and then we'll append with dot equals will append space and then goodbye world and then what we have to do is put it back so file put contents and then we're going to pass in here the file name and then current okay so let's go ahead and run that and now if we go back to file one text we get hello world goodbye world which I misspelled but that's fine all right so that's all we have to do to append now there's a lot of other things that you can do with files but what I want to show you next is to use the F open command so basically what that will do is we it allow us to put a file into a variable called a handle and then we can run continuous functions on that handle so let me give you an example let's say let's say we want to open a file for reading so we could say handle and set that to F open and then we'll pass in the file name okay and that actually takes another parameter which is what you want to do with it whether you want to write to it read to it both we're going to put an R because we want to read to freed from it and then we'll say data equals and then we're going to use a command called F read and then pass in the handle and then we can pass in the file size and pass in the file name like that okay and then we'll take that data and let's just echo it reload and we get what we have written written in the file I'm just going to fix that okay so that is F open and F read now we can also write to a file this way let's say open file for writing so again we're going to create a handle and set that to F open pass in the file name except this time we want to write to it and then let's say we want to I guess we could create a variable called text and we'll just say I don't know John Doe and then we can use F right and we're going to pass in the handle and then pass in the text and then you should also close the file when you're done so for that you can use F close and just pass in here the handle all right we should do that up here too all right I'm actually going to let's let's not use file one we can create the file from doing this as well so instead of that let's say file - dot txt alright so that should create the file right to it and then close it so save and reload and now over here if you look we have file to dot text and it has John Doe in it okay and we can continuously write to this if we want so let's say we have text equals Steve Smith we could again do the F right so now if we reload and go to file - we get John Doe Steve Smith now if we wanted to put this on a new line we could use a newline character here so actually you know what let's use double quotes here because if we use double quotes we can put this right in like that okay backslash n do the same thing here okay so we'll reload and go to file - txt and now you can see they're on different lines all right guys so I think that's going to be it for now you get quite a few different functions here that pertain to files and folders so I would encourage you to experiment with it maybe create your own little application that logs data to a text file or something like that maybe use a form maybe create a form that you can make a post request and then have that data log to the file or something like that I don't know I'm just giving you some ideas alright so thanks for watching guys please leave a like if you liked it subscribe if you're not already subscribed and thanks for watching
Info
Channel: Traversy Media
Views: 52,664
Rating: undefined out of 5
Keywords: php, php filesystem, fread, fwrite, fopen, php files, php file functions, php functions
Id: sLLZU38Okgo
Channel Id: undefined
Length: 18min 48sec (1128 seconds)
Published: Sat Apr 01 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.