I have a website hosted on a PC I have no access to. I have an upload form allowing people to upload mp3 files up to 30MB big. My server side script is done in PHP.
Below is my code where the user can upload a file. What I want to know is that is there a way so that via server side is there a way to first of all restrict the file formats of the files to jpeg and png only and then when the user clicks on the submit button, if the file format is correct then display an alert on the same page stating 'File is correct' else display an alert stating 'File is incorrect'.
Can somebody please provide coding if they know how to do this. Thank you and any help will be much appreciated :)
user1181690user11816904 Answers
A code for a total check of file uploads, you'll have to change $allowedtypes though. (Copied instead of linking because it was from a non-English site)
Travesty3You are talking about server side handler and write 'alert'...khm...If u want to do stuff via server-side, then use php handler
If u want to do stuff via client-side, use javascript events, e.g on change event
You have file extension in temp var.
PavePaveThere are PHP functions to do this. You want to look at mime_content_type and finfo_file. These are built-in PHP commands that allow you to interpret that actual file type of a file being uploaded. You can then filter the mime types to only .gif/.jpg/etc. You want to check the mime types over the file name because the file name can be changed to mask the actual file type. If you want code samples, there are plenty on those pages as well as some excellent user-provided alternatives.
thepip3rthepip3rSomething like this at the top of your file should work: