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!
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 . "." . "*";
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.
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.
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
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
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.
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.
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?
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?
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 ;)
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.
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?
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
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?
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.
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;
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 ?!
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?
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!
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?
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 :)
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 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.
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..
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?
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..
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?
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;
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!
this fixed the problem all above are complaining about!!! you deserve more likes to be up top
Thanks, that was a neat fix!
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 . "." . "*";
I have to say you are GOOD teacher......................Thanks
... and of course: thanks for your great tutorials. As a newbie I'm learning a lot. Keep up the good work!
The best
The best
The best
You are the best php tutor
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.
and ofcourse thanks for your awsome tutorials you are good teacher dude
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.
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
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
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.
same thought. And yes it works.
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.
Thank you so much sir.your videos are very helpful.
Awesome Awesome Awesome😍😍
hey dear Daniel what are these extra codes and funcs??
you could just use session to update status and return default image
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?
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?
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/
Its a good habit using camelCase naming for variables.. only bcs of its easy readability for programmers..
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 ;)
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.
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?
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
Thank you, I have added it to my to-do list :)
you could add the ' . ' before the * so it will be forced to find only the extension and not another pic with double 1
Thanks :D, You are awesome !!!
Is there any way to delete without having the if query and echo statements that it has or hasn't been uploaded?
Thanks Dani
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?
The recommendation is yes
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.
Thank you very much
After deleting image why didn't we get" file was deleted " echo statement in if statement of unlink
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;
This video goes through that
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 ?!
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?
www.codexworld.com/embed-pdf-document-file-in-html-web-page/
;)
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.";
thanks dani
Please can you make a video on how to create a user profile page.
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!
did you manage to solve the issue? im kinda stuck with the same problem here
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?
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 :)
mmtuts But what is the problem of a file being loaded couple of times on a website?
It takes up loading time which decreases your websites usability and search engine optimization (SEO).
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.
omg i have the exact bug
@@abdulrahmann1621 haha yep, bugs everywhere bugs bugs bugs. Just F php man, learn javascript, react, node.js uploading images to firebase or AWS
Is it more advanced than php in the back end or more easier or what?
Ivelin Ivanov or you’re talking just about uploading images?
@@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.
can i update my profileimage from .jpg to .jpeg??
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..
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.
plzzz make episodes on get data in api with save data in database for single array and multipal array .....
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?
i keep getting "undefined array key 0" for $fileName! Help!
Hi, did u solve your problem?
Do u need help?
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..
why not use prepared statement?
why doesnt show the photo, when i click sign up. :(
when i click sign up
"YOU HAVE AN ERROR"
Hi, did you solve your problem?
may I help you
maybe you can make a tutorial on how to create a search form.
I will put it on my to-do list :)
A good video. Could you please make Image uploding to blob in mysql in near future
I will put it on my to-do list ;)
+mmtuts That will make my day
Take your like god man
and a episode on navigation bar
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..
Ain't $fileinfo[0] == $file ?
But localhost only
it's good
nice
If you want too I'll show how
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?
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;
I heared PHP will die,is that exactly?
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.
PHP Is kinda a bit similar to Lua scripts
0 is false and 1 is true ;)