53: Delete profile image using PHP - PHP tutorial

Поделиться
HTML-код
  • Опубликовано: 8 сен 2024
  • 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 лет назад +12

    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 лет назад +3

      this fixed the problem all above are complaining about!!! you deserve more likes to be up top

    • @rigelr5345
      @rigelr5345 5 лет назад +1

      Thanks, that was a neat fix!

  • @omranshahrour7291
    @omranshahrour7291 2 года назад +1

    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 года назад +1

    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.

  • @matthiasdreyer5402
    @matthiasdreyer5402 6 лет назад

    ... and of course: thanks for your great tutorials. As a newbie I'm learning a lot. Keep up the good work!

  • @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

  • @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 ;)

  • @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.

  • @kingsleychibuzor8671
    @kingsleychibuzor8671 7 лет назад

    The best
    The best
    The best
    You are the best php tutor

  • @devwithbrian1534
    @devwithbrian1534 Год назад

    Thanks Dani

  • @yasinmonkezi5475
    @yasinmonkezi5475 4 года назад

    and ofcourse thanks for your awsome tutorials you are good teacher dude

  • @shashwatpratap6379
    @shashwatpratap6379 7 лет назад +9

    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 лет назад +3

      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.

  • @akashgola2153
    @akashgola2153 5 месяцев назад

    Awesome Awesome Awesome😍😍

  • @emanmazenrb2838
    @emanmazenrb2838 3 года назад

    Thank you very much

  • @modemaxit
    @modemaxit 5 лет назад

    Thank you so much sir.your videos are very helpful.

  • @yousufsulaiman2211
    @yousufsulaiman2211 3 года назад

    thanks dani

  • @turbosardar39
    @turbosardar39 4 года назад +3

    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.

  • @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 лет назад +2

      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.

  • @ao_no_yaiba3725
    @ao_no_yaiba3725 6 лет назад

    Thanks :D, You are awesome !!!

  • @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

  • @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

  • @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.

  • @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?

  • @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?

  • @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 ?!

  • @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

  • @Desdroyer24
    @Desdroyer24 5 лет назад

    Take your like god man

  • @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?

  • @mwangazatv1800
    @mwangazatv1800 6 лет назад

    it's good

  • @NewsouthmoviesHindi-pu2cs
    @NewsouthmoviesHindi-pu2cs 6 лет назад

    plzzz make episodes on get data in api with save data in database for single array and multipal array .....

  • @movine2344
    @movine2344 6 лет назад

    nice

  • @ivelinivanov3414
    @ivelinivanov3414 7 лет назад +1

    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.

  • @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

  • @The_Ashwatthama
    @The_Ashwatthama 6 лет назад

    After deleting image why didn't we get" file was deleted " echo statement in if statement of unlink

  • @balbasore69
    @balbasore69 4 года назад

    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?

  • @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..

  • @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..

  • @jaycelila6258
    @jaycelila6258 6 лет назад

    why not use prepared statement?

  • @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?

  • @TheNivesh19
    @TheNivesh19 7 лет назад

    and a episode on navigation bar

  • @angelinecaponpon9065
    @angelinecaponpon9065 6 лет назад

    If you want too I'll show how

  • @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 лет назад +1

      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.";

  • @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..

  • @plexodus1435
    @plexodus1435 7 лет назад

    maybe you can make a tutorial on how to create a search form.

  • @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 лет назад +1

      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 лет назад +1

      It takes up loading time which decreases your websites usability and search engine optimization (SEO).

  • @naveenkr5992
    @naveenkr5992 4 года назад

    Ain't $fileinfo[0] == $file ?

  • @jessephrolandcatalan7824
    @jessephrolandcatalan7824 7 лет назад

    can i update my profileimage from .jpg to .jpeg??

  • @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;

  • @neon-7777
    @neon-7777 3 года назад

    i keep getting "undefined array key 0" for $fileName! Help!

  • @aaronangeles2119
    @aaronangeles2119 7 лет назад +1

    why doesnt show the photo, when i click sign up. :(

  • @phemelomogatusi8495
    @phemelomogatusi8495 7 лет назад

    A good video. Could you please make Image uploding to blob in mysql in near future

  • @angelinecaponpon9065
    @angelinecaponpon9065 6 лет назад

    But localhost only

  • @hsjsjssjzjs1757
    @hsjsjssjzjs1757 6 лет назад

    PHP Is kinda a bit similar to Lua scripts

  • @saporojack613
    @saporojack613 7 лет назад

    I heared PHP will die,is that exactly?

    • @Dani_Krossing
      @Dani_Krossing  7 лет назад +2

      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.

  • @ademineshat
    @ademineshat 5 лет назад

    0 is false and 1 is true ;)