How to Delete Files From Folder with PHP

today we're going to learn how to delete files me root folder inside your website so as you guys can see inside my index PHP file have a very basic setup inside my body tags have a form which goes in and activates the file called delete file the PSP and this is going to be to find that we activate when we want to delete some kind of file from root folder now inside my root folder want to take a look at two files in here i have an index PHP file which is one we have in here and i have a folder with a file called cat the jpeg now this cat jpg is the 1i want to delete wanted to actually click the button so what I can do in here is we can go ahead and create the file called delete file the PHP which is going to delete the cabinets so i'm going to go ahead and open up new file I'm going to save it as delete file got a PSP and inside this file the first thing we're going to do is we're going to go and open up the PHP tags so inside the PHP tags we just need to include two things which include the path to defy want to delete and we also need to include the function that actually goes in and delete the file so the first thing we're going to do is we're going to set a variable called path we're gonna set it equal to double quotes then semicolon and then inside the double quotes are going to write uploads at least my case because my images inside and uploads folder /a cats dot jpg so right now we just include the path to the image that I wanted to delete or in case defy you guys want to be from your root folder and afterwards we just need to activate it so inside PHP we have a function called unlink which is the function that we use in order to delete files from the root folder so forward to go ahead and write on link parentheses and then include the path inside the parentheses we now have the function and goes in and delete the image now there's one more thing we can do because what if we get an error message when we want to find the image we can actually go ahead and write an if statement XO and inside the condition we can include the unlink function we have down here like so delete the semicolon and in order to get an error or not we can include an ! found on the function so we check if we did not manage to unlink this file up here then write this error message down here you can actually echo a string called you have and error or something and then under the if statement can run l statements that goes in and writes a success message if we actually deleted the file i'm just going to copy the echo here and say you have deleted your file like so and this is basically what we need in order to delete a file now we can also do if you guys don't want to have you know some kind of success message is you can also go ahead and write a cattle function so we can actually go back to the original page and just get some kind of success message inside the UL you can actually go into header function right double quotes location call one index dot PHP ? the leaks success so now if i want to go back inside a browser refresh and then delete the file you guys can see right now inside the URL at least from what I can see it says delete success if i go inside my root folder you guys considered now the cat-demons gone and that's because it just needed to file so this is how we can delete files from a website and if you guys are interested and how to upload files as you have to do that episode 62 so if you guys want to see you guys can go back in my videos so this is how we can delete files is very simple of course we can do more complicated stuff let's say don't know the extension of the file i want to delete let's say you allow users to upload all sorts of images to your website whether being jpg or PNG images then what we have here is not going to suffice because right now we set that it has to be a file called dot jpg but if we don't know the the type of file they want to delete then we need to do it slightly different so going to talk about how do that next episode so we got enjoyed and I'll see you guys next time