53: Delete profile image using PHP - PHP tutorial

  Рет қаралды 46,273

Dani Krossing

Dani Krossing

Күн бұрын

How to delete profile images from users - Learn PHP backend programming. Today we will learn how to delete profile images from our users, based of the previous episode.
➤ GET ACCESS TO MY LESSON MATERIAL HERE!
First of all, thank you for all the support you have given me!
I am really glad to have such an awesome community on my channel. It motivates me to continue creating and uploading content! So thank you!
I am now using Patreon to share improved and updated lesson material, and for a small fee you can access all the material. I have worked hard, and done my best to help you understand what I teach.
I hope you will find it helpful :)
Material for this lesson: / lesson-material-42361704

Пікірлер: 89
@esma_55
@esma_55 6 жыл бұрын
There's a slight glitch when trying to upload between different file extensions. This done the trick for me: foreach(glob("uploads/profile{$id}.*") as $match) { unlink($match); } ^ I placed that immediately before calling the move_uploaded_file method. Great tutorial by the way!
@realSkepSys
@realSkepSys 5 жыл бұрын
this fixed the problem all above are complaining about!!! you deserve more likes to be up top
@rigelr5345
@rigelr5345 5 жыл бұрын
Thanks, that was a neat fix!
@omranshahrour7291
@omranshahrour7291 2 жыл бұрын
At 11:08 when he wanted to solve the problem of getting the file exclusively from the uploads folder.. you just can put a dot in the filename variable so it means anything after the dot or (whatever the extension is ) after profile + id + . + EXTENSION and so that you preserve the compiler from being confused choosing the specific image, like so: $filename = "uploads/profile" . $sessionid . "." . "*";
@abdulrahmann1621
@abdulrahmann1621 4 жыл бұрын
Hi denial! I found a bug. If you upload one type of image lets say jpg and then you upload another type on top of it lets say png you will have 2 files uploaded to the server. Only 1 will show of course and then when you press the delete button once it will delete only the 1st found file and it will go back to the default profile picture but you will still have 1 more file uploaded to the server. You have to press the delete button again to delete the second file. It would be cool if you make a video on how to fix that :) Thank you.
@trueishere8302
@trueishere8302 6 жыл бұрын
I have to say you are GOOD teacher......................Thanks
@pingrash
@pingrash 5 жыл бұрын
Wasn't able to get glob to work for whatever reason but before watching this episode I had already added a column to the profileimg table to store the extension (inserted during image upload) so I was able to use sql statements to grab the extension from the table instead. Requires a few more lines of code compared to the glob method but worked all the same. Also had to update the column to null after deleting the image. I still need to figure out why glob wasn't returning any results though. It looks like a very handy method to know.
@shashwatpratap6379
@shashwatpratap6379 7 жыл бұрын
Hello Daniel, so i was thinking... Instead of doing all that complicated stuff to make sure you don't get profile11 in profile1 while globbing, why not just pass profile1.* instead of profile1* to the glob function? So your glob expression would be something like $filename = "uploads/profile".$sessionid.".*"; This would make sure that you have the desired file name, right? Do reply if you read this :D
@shashwatpratap6379
@shashwatpratap6379 7 жыл бұрын
So just to clarify for other people, my suggestion was to pass "uploads/profile1.*" instead of "uploads/profile1*" to the glob function, and it worked out! :D
@atatopatato
@atatopatato 5 жыл бұрын
a more simpler approach would be instead of status column inside the db, to actually store the full image path plus name plus extension or null if the user didn't upload any images. So when you want to display or delete the image, u simply grab the location of the image via the database.
@eb4416
@eb4416 4 жыл бұрын
same thought. And yes it works.
@silvanbusuttil
@silvanbusuttil Жыл бұрын
you could add the ' . ' before the * so it will be forced to find only the extension and not another pic with double 1
@kingsleychibuzor8671
@kingsleychibuzor8671 7 жыл бұрын
The best The best The best You are the best php tutor
@Hiturion
@Hiturion 4 жыл бұрын
A tiny improvement could be made: if you have a image named "my.profile1.jpg" Picking the [1] element of the array after the EXPLODE is going to return "profile", not ".jgp" extension. We should return the last element of the arraw to make sure the extension is extracted. Would make your code a bit more robust ;)
@matthiasdreyer5402
@matthiasdreyer5402 6 жыл бұрын
... and of course: thanks for your great tutorials. As a newbie I'm learning a lot. Keep up the good work!
@naushadjadoon6482
@naushadjadoon6482 7 жыл бұрын
hi, i love your videos on php and thank you for help thousands of programmers. Can you please make a video on how we can Authenticate a user from Microsoft Active Directory because its the main thing nowadays as organizations want their employees to get authenticated from AD before using their intranet websites.
@marekvanicek5286
@marekvanicek5286 5 жыл бұрын
As stated below, if u upload 2 images from 2 different users, the extension stays the same from the first image. I know that u usually dont help people with their coding, but this is an error in the actual video and not my code. Id greatly appreciate the response. Loved the tutorial btw!
@votum6595
@votum6595 2 жыл бұрын
did you manage to solve the issue? im kinda stuck with the same problem here
@devwithbrian1534
@devwithbrian1534 Жыл бұрын
Thanks Dani
@turbosardar39
@turbosardar39 4 жыл бұрын
No disrespect sir, But rather than writing all of that code we can save image name in profileimg table that it's easy to delete and upload.
@modemaxit
@modemaxit 5 жыл бұрын
Thank you so much sir.your videos are very helpful.
@matthiasdreyer5402
@matthiasdreyer5402 6 жыл бұрын
Hi Daniel, I was thinking why would you assemble the file name in a complicated way (1st explode() then concatenate) if you receive the complete filename of the profile image from glob() as $fileinfo[0]? I'm referring to index.php line 25-28 and deleteprofile.php line 7 to 11?
@yasinmonkezi5475
@yasinmonkezi5475 4 жыл бұрын
and ofcourse thanks for your awsome tutorials you are good teacher dude
@matejbasic7770
@matejbasic7770 7 жыл бұрын
Great video as always. I was wondering, do you have any plans on making a bigger project like we did with the login system and the comment section just writting it only using Object - oriented PHP?
@yasinmonkezi5475
@yasinmonkezi5475 4 жыл бұрын
hey dear Daniel what are these extra codes and funcs?? you could just use session to update status and return default image
@Vlad-ob2up
@Vlad-ob2up 3 жыл бұрын
on the episode in which we developed a log in system, we used prepared statements because we worked with sensitive data like passwords. as a best practice, should i always use prepared statements when working with queries inside php?
@DavidAshby1
@DavidAshby1 3 жыл бұрын
The recommendation is yes
@emanmazenrb2838
@emanmazenrb2838 3 жыл бұрын
Thank you very much
@dmlamarre1
@dmlamarre1 7 жыл бұрын
With multiple profiles it shows multiple images and if the images are of a different extension, only the extension type of the logged in user is shown.
@TheNivesh19
@TheNivesh19 7 жыл бұрын
plzzz make episodes on creating social network site plzzz as i watch lots of tutorials but i find that yours explanation are best and easy to understand so plzzz
@Dani_Krossing
@Dani_Krossing 7 жыл бұрын
Thank you, I have added it to my to-do list :)
@mrencryption9919
@mrencryption9919 4 жыл бұрын
Please can you make a video on how to create a user profile page.
@akashgola2153
@akashgola2153 6 ай бұрын
Awesome Awesome Awesome😍😍
@atulyadav1159
@atulyadav1159 7 жыл бұрын
the last lecture was how to upload images and other files and next lecture is how to delete files. I have missed the in-between lecture related to profile images so is it possible to get files of those lectures or maybe of all lectures.
@yousufsulaiman2211
@yousufsulaiman2211 3 жыл бұрын
thanks dani
@ivelinivanov3414
@ivelinivanov3414 7 жыл бұрын
I found a bug. If you a upload one type of image lets say jpg and then you upload another type on top of it lets say png you will have 2 files uploaded to the server. Only 1 will show of course and then when you press the delete button once it will delete only the 1st found file and it will go back to the default profile picture but you will still have 1 more file uploaded to the server. You have to press the delete button again to delete the second file. It would be cool if you make a video on how to fix that :) Thank you.
@abdulrahmann1621
@abdulrahmann1621 4 жыл бұрын
omg i have the exact bug
@ivelinivanov3414
@ivelinivanov3414 4 жыл бұрын
@@abdulrahmann1621 haha yep, bugs everywhere bugs bugs bugs. Just F php man, learn javascript, react, node.js uploading images to firebase or AWS
@abdulrahmann1621
@abdulrahmann1621 4 жыл бұрын
Is it more advanced than php in the back end or more easier or what?
@abdulrahmann1621
@abdulrahmann1621 4 жыл бұрын
Ivelin Ivanov or you’re talking just about uploading images?
@ivelinivanov3414
@ivelinivanov3414 4 жыл бұрын
@@abdulrahmann1621 no i mean in general. It's just easier in my opinion. And more newer and up to date with technology. Most new projects and companies work with these technologies. PHP is used mostly for legacy projects that have been written long time ago in PHP and need mantaining. There's many libraries, packages for nodejs that are up to date and make things easier.
@victoriadonaghy4445
@victoriadonaghy4445 6 жыл бұрын
Is there any way to delete without having the if query and echo statements that it has or hasn't been uploaded?
@gabrielfono844
@gabrielfono844 3 жыл бұрын
hi sir please in your previous , you have mentioned at one of the comments that you will show us in this video how to allow multiple extension since jpg was the only extension we could upload. I haven't seen anything like that in your patreon. I guess you might have forgotten please I need your help and look forward to hearing from you back;
@DavidAshby1
@DavidAshby1 3 жыл бұрын
This video goes through that
@amoghdatar2726
@amoghdatar2726 6 жыл бұрын
13:07 may I ask you why aren't you now not naming the variables camel case? like: $fileactualext instead of $fileActualExt? And will you please tell what is the best way to name variables?
@unzudgeo
@unzudgeo 6 жыл бұрын
Hi Amogh, if you google for the style guide for the language you are interested in e.g. here is one for PHP www.php-fig.org/psr/psr-2/
@rejilradhakrishnan1072
@rejilradhakrishnan1072 5 жыл бұрын
Its a good habit using camelCase naming for variables.. only bcs of its easy readability for programmers..
@NewsouthmoviesHindi-pu2cs
@NewsouthmoviesHindi-pu2cs 6 жыл бұрын
plzzz make episodes on get data in api with save data in database for single array and multipal array .....
@omranshahrour7291
@omranshahrour7291 2 жыл бұрын
There's one logical error when the user uploads two images with different extensions the code won't change the first one but add the second one to the uploads folder. For instance, when uploading .jpg it'll works well, but then when deciding to change the image to .png, now the uploads folder will have two images profile1.jpg & profile1.png So we have to delete the file has already inserted and replace it with the new one. but how ?!
@melvin9198
@melvin9198 7 жыл бұрын
Hi Daniel! I'm sorry if I've missed anything, but what does "include_once" means? Does it means that you only include the file once, and why do you do that if that is the case?
@Dani_Krossing
@Dani_Krossing 7 жыл бұрын
Hi there! Here is a short explanation. include() simply includes a file into your document. include_once() first checks if the file has been included previously in your code. If so it gives an error message. If not it simply includes the file into your document. This is to make sure that your website doesn't load the same files multiple times. Hope this helped :)
@melvin9198
@melvin9198 7 жыл бұрын
mmtuts But what is the problem of a file being loaded couple of times on a website?
@Dani_Krossing
@Dani_Krossing 7 жыл бұрын
It takes up loading time which decreases your websites usability and search engine optimization (SEO).
@jlwebcreative
@jlwebcreative 6 жыл бұрын
Loving the tutorials Daniel. Am just trying to upload PDF, but as it's not an image, it won't show. It does show in the uploads folder but how would I get this to work and add a link, so that the PDF opens in the browser?
@Dani_Krossing
@Dani_Krossing 6 жыл бұрын
www.codexworld.com/embed-pdf-document-file-in-html-web-page/ ;)
@jlwebcreative
@jlwebcreative 6 жыл бұрын
Thanks for such a speeeeeedy reply! I'm trying this line of code, which seems to work, but not very elegant. What do you think? echo "View My CV.";
@The_Ashwatthama
@The_Ashwatthama 6 жыл бұрын
After deleting image why didn't we get" file was deleted " echo statement in if statement of unlink
@VetriselvanT18
@VetriselvanT18 4 жыл бұрын
mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, array given in ....I'm getting this error on line while($rowImg = mysqli_fetch_assoc($resultImg)){...}can anyone help me out..
@ao_no_yaiba3725
@ao_no_yaiba3725 6 жыл бұрын
Thanks :D, You are awesome !!!
@daniellajohansson
@daniellajohansson 7 жыл бұрын
If you have a *profile11.jpg* in your folder the code in index.php will not work. It will show the broken img for images you upload with other extensions then .jpg Just wanted to say..
@balbasore69
@balbasore69 5 жыл бұрын
this works fine. simple yet effective. $filepath = "uploads/profile".$sessionid.".*"; $fileinfo = glob($filepath); if(!unlink($fileinfo[0])){ echo "Error deleting the profile image."; } else { echo "File was deleted."; } the problem is what if you have different files like profile1.docx, profile1.pdf, profile1.png, profile1.jpg, are they all be deleted?
@angelinecaponpon9065
@angelinecaponpon9065 6 жыл бұрын
Hi sir Daniel, I like it . And hello every one I have something new ??? I have SQL and php in my android phone. I'm not joking this true man promise..
@gadaacode
@gadaacode 5 жыл бұрын
Instead of doing all that complicated stuff, What if we only update the 'status' inside 'profileimg' table to 1 and leave other operation when delete profile pic is clicked?
@Desdroyer24
@Desdroyer24 5 жыл бұрын
Take your like god man
@plexodus1435
@plexodus1435 7 жыл бұрын
maybe you can make a tutorial on how to create a search form.
@Dani_Krossing
@Dani_Krossing 7 жыл бұрын
I will put it on my to-do list :)
@TheNivesh19
@TheNivesh19 7 жыл бұрын
and a episode on navigation bar
@phemelomogatusi8495
@phemelomogatusi8495 7 жыл бұрын
A good video. Could you please make Image uploding to blob in mysql in near future
@Dani_Krossing
@Dani_Krossing 7 жыл бұрын
I will put it on my to-do list ;)
@phemelomogatusi8495
@phemelomogatusi8495 7 жыл бұрын
+mmtuts That will make my day
@jessephrolandcatalan7824
@jessephrolandcatalan7824 7 жыл бұрын
can i update my profileimage from .jpg to .jpeg??
@mwangazatv1800
@mwangazatv1800 7 жыл бұрын
it's good
@neon-7777
@neon-7777 3 жыл бұрын
i keep getting "undefined array key 0" for $fileName! Help!
@yousufsulaiman2211
@yousufsulaiman2211 3 жыл бұрын
Hi, did u solve your problem? Do u need help?
@angelinecaponpon9065
@angelinecaponpon9065 6 жыл бұрын
If you want too I'll show how
@jaycelila6258
@jaycelila6258 6 жыл бұрын
why not use prepared statement?
@aaronangeles2119
@aaronangeles2119 7 жыл бұрын
why doesnt show the photo, when i click sign up. :(
@aaronangeles2119
@aaronangeles2119 7 жыл бұрын
when i click sign up "YOU HAVE AN ERROR"
@yousufsulaiman2211
@yousufsulaiman2211 3 жыл бұрын
Hi, did you solve your problem? may I help you
@movine2344
@movine2344 6 жыл бұрын
nice
@remyreijven
@remyreijven 6 жыл бұрын
What would happen if user 1 has just deleted its profile image and hits the delete-button another time or manually browses to the deleteprofile.php page? $fileinfo[0] would then result in the profile image of user 11, which gives user 1 the opportunity to delete the image of user 11. To prevent this from happening, one could improve deleteprofile.php by checking whether status is set to 0 for the current userid in the profileimg table, before taking any other actions. Alternatively, one could add the extension's seperator (.) to the $filename variable, i.e. $filename = "uploads/profile".$sessionid.".*"; // with the extension's seperator (.) before the * to eliminate the entire '1, 11, 111, ...' issue. When choosing this solution, you also have to remove the extension's seperator (.) in the $file variable, i.e. $file = "uploads/profile".$sessionid.$fileactualext;
@naveenkr5992
@naveenkr5992 4 жыл бұрын
Ain't $fileinfo[0] == $file ?
@hsjsjssjzjs1757
@hsjsjssjzjs1757 6 жыл бұрын
PHP Is kinda a bit similar to Lua scripts
@angelinecaponpon9065
@angelinecaponpon9065 6 жыл бұрын
But localhost only
@ademineshat
@ademineshat 5 жыл бұрын
0 is false and 1 is true ;)
@saporojack613
@saporojack613 7 жыл бұрын
I heared PHP will die,is that exactly?
@Dani_Krossing
@Dani_Krossing 7 жыл бұрын
Hehe no PHP is not gonna die out for a while :) A lot of famous systems today are written in PHP such as Facebook and WordPress.
54: How to delete files from folder - PHP tutorial
4:22
Dani Krossing
Рет қаралды 59 М.
Men Vs Women Survive The Wilderness For $500,000
31:48
MrBeast
Рет қаралды 99 МЛН
Как мы играем в игры 😂
00:20
МЯТНАЯ ФАНТА
Рет қаралды 1,4 МЛН
55: Delete more than one file or image in PHP - PHP tutorial
15:16
Dani Krossing
Рет қаралды 36 М.
5 Signs of an Inexperienced Self-Taught Developer (and how to fix)
8:40
Insert Multiple Images into Mysql Database using PHP Ajax
18:24
Webslesson
Рет қаралды 24 М.
Vim in 100 Seconds
11:53
Fireship
Рет қаралды 1,1 МЛН
Should You Learn Object Oriented PHP?
13:48
Dani Krossing
Рет қаралды 10 М.
Men Vs Women Survive The Wilderness For $500,000
31:48
MrBeast
Рет қаралды 99 МЛН