Delete Profile Image Using PHP

today we're going to learn how to delete files from our website and it's going to be all types of files such as images or text files you know any kind of funny might have inside the root folder we're going to learn how to delete these in this episode now before we do that and before we do any coding i want to mention one thing now what I have in front of me here is going to be based off the previous two episodes we did on how to upload files and how to upload profile images now even though you guys may not have followed the previous two episodes this lesson we'll still teach you how to delete files from a website so if you have your own website and from you right now and you just want to look for a quick way to delete files we're going to do that in this episode ok so don't need to have my specific code in front of you now if you guys want to have this code you want to follow this lesson I'm going to go to leave a link in the description of this video so you guys can download the files and follow along on this lesson here so now we know this i'm going to go to show you guys what we created in the previous episode just we know what exactly want to do today so inside this browser here i have a user up in the top here with a username and then you know a name and a profile image now right now I'm actually locked in us to use so i can also lock out if I want to if I love in as this user here i'm going to choose a file and upload a new profile image let's say I want to be this guy down here upload as you guys can see it not changed the profile image now whatever want to delete the profile image and just have a default limits up here now if you guys can see it actually went ahead and create another form down here that the leads to profile image so click the delete button here you guys can see now rewards back to the default image we have when we do not have a profile image and I did that by changing the image and i went ahead and delete the profile image that i uploaded before you noted the picture with the guy in here because we don't want to you know just change the profile image back to default one we want to actually delete the file from a root folder in order to actually change it so just to show you guys to go inside my uploads folder we have all the profile images if i go ahead and you know choose another image this guy for example upload him if we're going to my root folder inside my uploads folder you guys can see we have a profile image if i go ahead and click the lead go back inside of it now I guess can see the profile it is gone so we're going to learn how to delete files from the root folder so what we're going to do now is I'm going to go ahead and go inside my index 2.php file and inside the index the PSP file I'm going to go down to where we have our upload form because underneath the upload form we're going to create a delete form i'm going to go ahead and copy my echo down here that says you know form accidents to upload the PSB file going to go ahead and paste it right underneath it now what we're going to do here is I'm going to go ahead and delete the input we have inside of it because we don't need an input we just need to you know just load a file in order to delete this profile image so inside the action here i'm going to change the name to delete profile or something else i'm going to go ahead and delete the same time as well because we don't need that in here and i'm going to go ahead and change the text inside my button down here to delete profile image like so inside the text editor when according to create a new file and this file is going to be the one to delete the profile image from our root folder so if you guys wanna learn how to delete files this is what we're going to do it so inside this file going to save it as delete profile dot PHP and inside this file the first thing we're going to do is we're going to go ahead and warm up to PHP tags now inside the PHP tags we're going to go and start a session because in my example here we have a user was locked in in order to see the delete profile image button we need to be locked in so we need to have a session that actually runs so we can see who is locked in at the moment you want to say session start parentheses underneath here we're going to go ahead and include out database file because as you guys may know from the previous episode we registers inside the database if the user had a profile image uploaded or not so we need to change this to falls since you want to delete the profile image they won't need to set the database as the user has not a profile image uploaded so we're going to include on school once single quotes dbh dot PHP on the next line here we're going to go ahead and get decision ID from the user is currently locked in which at the moment is the admin inside my website so we're going to go ahead and say they'll assign session ID is equal to the current superglobal called session that has a name as ID so right now we have two session ID from the user is currently locked in so we know which profile image you want to delete now the next thing we need to do is you want to delete the profile image or these two file that we want to delete but before we can do that need to pinpoint the right file so we can actually select it and then delete it now because we're in the previous episode now and this doesn't count for you guys who did not follow the previous episodes but if you did as you guys may remember we did actually allow for more than one type of file to get uploaded such as jpegs jpa eegees PNG's and PDFs now what that means is that we want to pinpoint the file we need to actually go in select the file the extension here which is right now jpg could be PNG your PDF will be any kind of file so what we need to do is we need to say we have a file somewhere inside the uploads folder that has this base name but we don't know the extension yet so no to find a file that we don't know the extension of we're gonna have to write a bit of code and this is code that is really useful even though he might not have created a system we could upload Morton just a JPEG you know like PNG something so we need to do that first so what we're going to do here is we're going to go and create a variable called file name i'm going to set it equal to the path that we know the image is inside up so right now we're going to my root folder you guys can see I have a phone call uploads where I have the profile images you need to say okay you want to create a path for writing a string which is inside the uploads folder /a and then we have a name called profile and then as you guys can see if i go back inside the root for the gas can see how the one in front of this profile image and that's because the user here has an ID as one inside the database so we need to get the session ID of this user here which we already created up here so just copy it paste it right next to it on the right side and then what we need to do in order to actually get the extension because right now if you want to go in and find this specific file here is going to give me an error message it's because right now it doesn't know you know what is the file called profile one there's no such thing in there because right now it's called profile 1 dot jpg so what we can do here is we can actually go and say well we do also have another string and then inside the string we're going to write star a multiplication symbol was right now means that we have something going on behind this number here you know decision ideas right now to the file we're looking for is called profile one something which is what to start us so now we have this we can actually go ahead and search for the file so what I'm going to do here is I'm going to go and create another variable i'm going to call this one file info set it equal to co-op which is a function we have inside PHP the ghosts in and searches for a specific file that has part of the name that were looking for so we have a file is called profile one something which we have up here is going to go ahead and list out all the files that matches this search up here which in our case because we created in the certain way with the use ID there's only gonna be one file that has this ok now of course right now if I were to have 11 users inside my database you would actually have a user profile image in here says profile 11 now if we search for profile 11 in this case is going to go ahead and list up to users and the reason for that is that profile one which were searching for up here is going to be inside both profile images names now we will get around that i'm going to show you guys in just a second how we're not going to get an error message when we do actually you know get the image using drop so for now this actually just going to continue here i'm going to show you guys what I mean in a few seconds so inside the glob function here we want to get the file name because we're looking for a specific file up here so file info right now if i want to go down and print our this one here parentheses file info go inside my website refresh and then delete my profile image you guys will see we get an array right now the first data inside ra is equal to this path right here because it found a file called profile 1 dot jpg which is our profile image that we have so right now we did actually get the extension without having to type the dot a pic inside the path now we get one problem which is the one I mentioned before let's say have one you sent out the database which has a ideas 11 which had to actually happen here I did act create one that has an ID as 11 and he also uploaded a profile image now what it's going to look like if i go inside my uploads folder and just kind of copy the image i have in here and change the name to profile 11 which in this case is what it would actually be like you know how to upload a profile limits as user 11 if i go back inside my browser refresh the website and go back again you guys can see we get to results we get one piece of data inside this array called this file which is dr. a peg and then I get another piece of data which is also a search result that matches what i typed in which is equal to upload profile 11 because we just said we needed anything behind 21 so right now there's something that matches this is 1.jpg now the thing i want to point out here even though we have two results from the search the first one is always going to be 21 the gas is searching for if i were to search for uploads profile 11 will only get one result there will be this one over here so this one would actually have the data place 0 when this case we search for profile one which means it's going to get out both these results but the one we're looking for is always going to be the first one ok so it's really important you guys focus on that only need the first data result so what we can do now is we can actually go back delete the print our function down here because we don't actually need it and we're going to go and create another variable but we need to get the extension of this file here we're going to save file EXT is equal to explode because now we need to actually take this we have up here from the file info parentheses semicolon and then what we need to do is we need to insert two parameters we need to tell where we want to explode the string and we need to tell with string want to explode so right now we're going to right double quotes from creation because we want to take it apart right before we get the extension and then the second parameter is going to be the file info variable we have up here which in this case is going to have all the search results from district up here so what's really important to know here is that based off what I told you guys a few seconds ago we might get more than one result when we run this glob service here so we need to make sure going to get the first result for writing brackets and then inside the brackets we write 0 because now we only get the first result from our array that we get from the globe search up here so after we do this let's actually go ahead and print are what we just did here so is a print on school are parentheses semicolon and then we're going to print our the file extension variable we have up here just to show you guys what we're doing if i refresh the website to the profile limits you guys can see we get an array you know based off the explode function we did we get two different results we get the first data which is equal to uploads profile one and we get the second data right here which is equal to jpg so now we just got the extension from the file having the extension now we can go ahead and go down to the next line and say file actual extension which is equal to the file extension we have up here array that has the data as the second data which in this case would be one so right now file actual extension is equal to jpg ok so now we have the extension we can actually go ahead and go in and delete the file so what we need to do now is we need to write the full path to where we want to delete the file and the file name they want to delete because right now we just figured out what the file extension is we can actually write the complete file name of the file want to delete so down here we're going to say we have a variable called file which is equal to a path called uploads /s profile and then we want to get the session ID afterwards so it's actually basically the same as what we have up here we can actually just go ahead and copy what we have up here because that would be silly not to and instead of saying we have something going on after the session ID we can actually include the extension we just got from up here we're going to say we have profile 1 dot the file actual extensions now dot jpg so now we just got the right file name or different the right file extension and now we wrote the entire name of this file so now we have the entire file name we can actually go ahead and start deleting the file going to run if statement because we want to have an error message if we did actually managed not to delete a certain file so we want to know if we had some kind of arrows we're going to stay inside the if statement we have a ! on link parentheses now to unlink function here is going to be the one that goes in and delete the file so it did not manage to you know delete this specific file here called dollars on file which you need to insert inside the parentheses then it needs to give us an error message but l's then it needs to delete the file and maybe take us back to the front page of something so what we need to do here is we can actually go ahead and write the error message to the echo file was not the leaders or something then inside the else statement we can go ahead and make another echo to want to we can save file was deleted by the way we're not going to see these messages here because we're just gonna get taken directly back to the front page but if you want to test this out and not right ahead of function takes us back it's going to say one of these two thanks so we're just doing this because we want to run this function up you called on link which is being run right now even though it's inside an if statement so now we deleted the file we can actually go ahead and go down to next line and what we need to do now at least in our case based off the previous two episodes is we need to go inside the database and update our image profile table or profile image table which right now has a profile image set to true because my user uploaded profile limits before that we want to delete and because of that is right now set to true so need to change it back to false because it just deleted it so what we need to do here is we need to write a dollar sign is ql which is an SQL command want to run inside the database double quotes and then we want to run an update profile image which is the database table name set status which is the column that is equal to true or false in our case right now is equal to true because it says we have a profile image uploaded you want to change that to false going to be equal to one where user ID is equal to a current session ID that we just have up here so right now we're changing this at the user that has a user ID as the current session locked in s semicolon and then again semicolon now I did I to get asked before about this sinkhole and that i sometimes include and sometimes leave out of my SQL string now just to make it clear this right here is correct and this right here is also correct now some databases prefer that he closed off the SQL string that we have inside here with a semicolon so I recommended you always do it ok so after we just included the SQL string we wanted to run inside the database we're going to go ahead and actually run it i'm going to go ahead and write my SQL i underscore quarry which means 21 are querying this SQL string up here i'm going to paste in dallas on SQL and we're also going to include our database connection which we included up here inside this file in here we have a variable called con which is our connection to the database like so so now we just did is we updated our database after we update the database we want to be taken back to the front page I'm gonna ride ahead of function that says double quotes location col1 in books dots PHP and then after the PSP we're just going to include a dismal message so it says delete complete or success like so now we have this with just successfully deleted our profile image so what we can do now is we can actually go and test is there's one more thing we need to do at least if you created the you know the profile image upload system within the previous episodes there's one more thing we need to do which is that we need to change the extension that we actually show inside the browser because right now max is showing at a payments but what if we uploaded a pms when we upload a profile image you know we need to change that but if you did not follow the previous two episodes this is not something you need to worry about so let's actually go and test will just did if i go back inside my website and refresh you guys can see once i click the profile image we get the default image because we change the you know our user inside the profile image table to false so right now we do not have a profile image uploaded if i go back inside my website choose a profile image upload go back to the database you guys can see the status right now if the first row we have in here it's going to change 20 and again if i change it again to meet the profile image you guys can see it says false so we just successfully deleted a profile image and we changed a database column now if you guys follow the previous episode we did and how to upload profile images there's one more thing you need to change in order to show these images properly inside the browser and just one more thing I want to point out that did actually make a typo in the previous episode regarding the double quotes of single quotes down here we have the empty random function so if you guys didn't see that in the annotation in the previous episode because it disabled them i recommend gasps go back enable annotations and check for the you know the type of made here at least take it right now because this would be correct so what we need to do here is as you guys can see we did actually go in and say that the profile image that we uploaded was a JPEG image because we just use the jpeg as an example that previous episode but it didn't occur to me that we might upload a PNG image instead so right now for go inside the browser we have and I upload a PMT image instead let's actually go ahead and delete a profile image and then upload a PN TMS instead which could be this one here doesn't really matter just any kind of PNG you guys can see that we have no images displayed it does actually goes an error message because right now inside the front page it says we need to display a jpay commits so what we need to do here is we do actually need to do kind of the same thing as we did in our delete profile limits file here we went then and search for that specific profile limits and then we need to see if the extension was jpg or PNG or PDF or whatever so we need to copy all we have here in line 6 7 8 and 9 copy it go inside the index page and right before we start showing the image we need to insert these lines of code so we do actually get the proper extension adjustably copy it down here and we go inside the string with this dot jpg to delete the jpeg right double quotes twice because we also need the question mark behind it to punctuation and then we insert the file actual extension so now we get the proper extension and once we go back to the browser you guys can see we now get the proper profile limits now of course was going to get us an ID error message which is because right now we're doing we do not have the what-you-call-it the session ID variable down here but as you guys can see we do actually have the idea of here from the previous code we did in the previous episode so i can actually replace it with this code down here so do actually get the proper use ID save it and now we should have no error messages so now we can also go ahead and to beat the PNG image if you want to because we did actually create correctly as you guys can typical inside the root folder there's no p.m. it's as well so now we can upload all kinds of files and delete them again so this is how we can delete stuff from our website and i hope you guys found this useful and i'll see you guys next time