55: Delete more than one file or image in PHP - PHP tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in the last episode we talked about how to lead a file from our website and in this episode we're going to talk about deleting multiple files because you guys have been asking in the comments how to delete more than one file so what we're gonna do today is based off the previous lesson as you guys can see all the code I have in front of me here is directly the same as we did previously we're gonna go ahead and delete more than one file meaning we can type in multiple names and delete all those files at once so what I have in front of me here is two files I have the index page and I have the scripts that goes in and deletes the file so based off this code you guys in just go ahead and copy it here cuz there's not a lot of code if you don't have it yet we're going to go and delete multiple files so the first thing we're going to go ahead and do at least the way I want this to work is I want to include an input inside the form you created inside our index page so right now as you guys can see inside the browser we just have a delete button so what I want to do here is I want to include an input where we can type the name of the file we want to delete so what we're gonna do is I'm gonna go ahead and include an input I'm going to go ahead and give it a type as text I'm gonna set a name to file name and we're gonna go ahead and include a placeholder because we need to tell the users what we want them to do in order to delete more than one file so in here inside the place hold I'm gonna say separate each name with a comma then we just add maybe some parentheses with a comments out of it so they understand what they need to do and then afterwards we're gonna go ahead and include a styling inside the input because we want it to be slightly longer since this text we just included in here is quite long we need them to see everything we're gonna go and style it so I'm gonna save style equal to double quotes with so be 300 pixels so right now for what to go inside my browser refresh you guys can see we now get an input that says separate each name with a comma okay so what we can do now is we need to go ahead and do a little bit of error handling because if I were to say okay inside my website are these inside a root folder I have two files I have caulk at the JPEG and I have one called laptop the JPEG first of all if I were to type in a couple of names and I were to either use let's say cats dot JPEG space or maybe comma space laptop dot jpg as you guys can see we have not just a comma but because of habit from some users we also have a space so we need error handlers that goes in and checks if we included spaces or any kind of weird stuff and then eliminate it because we don't want to have this space in here when we do I just need to run the scripts inside the next page so what we can do here I'm just gonna go ahead and delete what we have here is I'm gonna go ahead and go inside our scripts which is in here and we're gonna go and do quite a few things because first of all we need to grab the data we have inside the input which right now is called filename I'm just gonna go and copy that go into our scripts and I'm gonna go and create a variable called let's just call it file name or names with an S behind it I'm gonna set it equal to dollar sign underscore post brackets and inside the post superglobal we're going to go and include the name file name because that's the name inside our input so after we have the name or at least the long string that the user typed in we can go ahead and replace all the spaces with no spaces because we need to eliminate all the spaces or any kind of other weird characters that we might think of inside whatever they type in so I'm gonna go ahead and say we have a variable called remove spaces is equal to a string or not a string but a method called string replace or string on the score replace parentheses now inside of this we need three different parameters first of all we need to tell it what are we searching for what do you want to replace it with and then afterwards we need to tell it which string we want to check so the first thing is to double quotes where we say okay if we find a space then we want to , double quotes replace it with no space so we're not gonna include anything inside these double quotes here and then afterwards we want to include file names because that's what we want to check and then replace all these spaces with so now that we have this we can actually go ahead and go down to the next line we need to separate each thing that they typed in using commas so what we're going to do down here is we're going to create a variable called all file names and set it equal to explode which is a function we have inside PHP that separates all the you know a string using specific characters so we have come as we separate the string all the places we have commas so inside the explode we're going to say we have a character called comma and the next parameter is going to be the string that we want to replace or the separate using commas so we're going to take our remove spaces and insert as the second parameter so right now what we actually have is if I were to go ahead and print this out inside the browser if I just comment out everything else we have underneath here if I were to do a print underscore R and do all filenames semicolon and then run this script inside my browser I can say caps dot jpg comma space laptop dot jpg and then run it you guys can see we now get an array which is down here we have two different data inside the first space we have captive jpg and inside the second data we have a laptop that jpg and as you guys can see you actually separated or at least removed two spaces and then separated each name you know using the comments so now that we that this we can actually go ahead and delete the print R because we don't actually need it and before we actually start deleting these files because now we have the name so we could essentially just do the same thing as we did down here using a loop but what if we type in a name that doesn't exist inside our root folder or inside the website we need to come out with an error message so what I'm gonna go ahead and do here before we start the leading files is I'm gonna go and create a for loop and this for loop is gonna go ahead and loop out each time we have a piece of data inside this array because right now it could be two pieces of you know two names you have inside the array or maybe the user tucked in six names inside the array we don't know so we need to make sure we loop out the number that we have inside the array of course right now we don't actually have the number so we need to get that as well so what I'm gonna do inside right before the for loop is I'm gonna go ahead and create another variable I'm gonna call this one counts all names and then I'm gonna go ahead and set it equal to something called counts which is a function we have inside PHP that actually counts how many pieces of data we have inside an array so I'm gonna go ahead and put all file names inside the counts which now counts out how many different pieces of data we have inside this array up here so now we can use this variable down here that counts number and set it in as you know the limit we want to loop so now that we have the limit we can go ahead and go inside the for loop that goes in and checks if we have a certain file inside our website and if you don't have a file you want to stop it from running the script war would inside right now and just print out some kind of error message so the way I'm gonna do this is I'm gonna go ahead and create an if statement and inside the condition we're gonna go and create a PHP function called file underscore exists parenthesis and what this one does is that it goes ahead and takes a certain path we might have to our file and checks if it does exist if it does not exist is going to return as false or if it does exist it's going to return as true so we need to make sure we don't get any false results using these loops here so what we're going to go ahead and do is I'm gonna go ahead and take the path we created in the previous episode which is down here called uploads forward slash capital JPEG I'm gonna paste it inside the function now because the name is not capped at JPEG but the name have inside the array that we created up here we're gonna go ahead and take this array at least the name of it and we're gonna replace it with the last part of the the path called Catlett jpg because right now we don't know the name it depends on what the user types in so we're gonna go ahead and say we want to delete it and then afterwards we're gonna include the variable we have up here called all file names now of course right now all file names is an array and we need to get each data meaning that we need to have brackets behind it and tell it which numbered data we want to check now the cool thing about the for loop is the right now it starts at 0 and then each loop is gonna add one number to it meaning that right now the first loop variable I up here is going to be equal to 0 so we can actually take variable I and put it inside the brackets because right now it's called all filenames 0 then the next loop is called all filenames 1 and it's only going to continue as many times as we have you know data inside disarray up here so after including the parameter inside the file access function we now need to check if it's going to return us true or false so we do that by setting it equal or equal equal to false because right now if you can't find any of the names or at least just one of the names we have inside the string we typed in then it's gonna go ahead and do whatever's inside the if statement so if we do not have the file that we want to delete then we want to return the user to the front page with an error message so we say header double quotes location colon index dot PHP question mark and then we say delete error so now inside the URL when we do actually get an error message because the file doesn't exist we get returned to the front page with this URL up here that says lead error so we can actually see if do actually have to file a not so now that we have this we need to do one more thing because if this if statement he runs we want to make sure that the script doesn't continue because what we're going to do is we're actually going to go ahead and continue the delete function outside the for loop so because of that we need to make sure we actually do end this script we have going on in here inside the entire page if we get one of these error messages so what we're going to do after the header function is we're going to go ahead and say exit parentheses which is a function inside PHP that simply exits the script and prevents anything else from running okay so now that we did this we just checked if this if the names do actually exist inside our website if it doesn't exist then it's going to give us an error message by returning us to the front page so the next thing we need to do is actually loop out the unlink functions that actually goes in and deletes the files because once we've done that and we make sure that all the files do actually exist because we just checked it we just deleted all the files that we typed inside the input so the way we're gonna do this is we're gonna go ahead and create another for loop right underneath the first for loop like so and we're gonna go ahead and run the exact same parameters inside the for loop so I'm just gonna copy what we have here and then inside the loop the first thing we're gonna do is we're gonna go ahead and copy everything we have down here which we created in the previous episode copy it and paste it inside the for loop and just move it out so it looks nice and what we need to do now is we need to make changes to the path because it needs to be the exact same thing as we did when we did actually do the file exists function up here so we need to go ahead and copy everything we have in there like so because right now because the for loop is the same it's also going to go through each file individually with this file name that we have here so doing that when we run this if statement down here it will in fact go in and delete each file now because we are running a loop now you know using a for loop we can't do the header function inside the loop otherwise it's gonna you know exit us from this document here at these exit from the script before we get done up deleting all the files inside the website so what we need to do here is we need to copy the header function and then delete the entire else statement we have here and then go down outside the for loop and paste it in now what we also need to do is because we're checking for errors right here because we also need to exit the scripts and we could instead of an echo we could also do the same thing as we did up here by you know creating a header function that returns us to the front page with an error message but I'm just gonna go and leave it the same as the last episode so this is basically all we need to do if I were to go back inside my website refresh type the name of the files have inside my root folder which right now is kept a jpg and laptop to jpg you guys will see that we do actually get these files deleted so I'm just gonna write cattle jpg comma space laptop dot jpg delete and as you guys can see it says delete success or maybe cannot see is because it's quite small up there but if I go inside my root folder you guys can see they're gone so this is how we can delete multiple files from a website and do bear in mind this is without using JavaScript which means that right now you know what you could do using javascript is you could go in and type one name of a file then when you're right comma you could include a second input underneath it it just pops up with this you know we can type in a second name if you wanted to but because we're not gonna use JavaScript in this episode because this is a PHP series I wanted to teach you guys how to do this only using PHP code okay so hope you guys enjoyed and I'll see you guys next time
Info
Channel: Dani Krossing
Views: 33,758
Rating: undefined out of 5
Keywords: delete multiple files in php, delete many files in php, delete more than one file in php, delete files in php, how to delete multiple files in php, how to delete many files in php, delete multiple files tutorial, delete multiple files php tutorial, delete multiple images in php, delete multiple files or images in php, delete multiple files or images, delete many files or images in php, delete more than one file or image in php, php, php tutorial
Id: q6l7KnRJQJU
Channel Id: undefined
Length: 15min 15sec (915 seconds)
Published: Wed Dec 21 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.