Php Upload File Server Side

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.

Active6 years, 8 months ago

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 :)

user1181690user1181690
3081 gold badge4 silver badges12 bronze badges

4 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)

Travesty3Server
12.9k4 gold badges45 silver badges89 bronze badges
axiomeraxiomer
2,0021 gold badge13 silver badges25 bronze badges
Php upload file script

You 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.

PavePave
1,1654 gold badges12 silver badges19 bronze badges

There 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.

thepip3rthepip3r
1,5104 gold badges23 silver badges29 bronze badges

Something like this at the top of your file should work:

Php Upload File Size Limit

Travesty3Travesty3
12.9k4 gold badges45 silver badges89 bronze badges

Php File Upload Mysql

Not the answer you're looking for? Browse other questions tagged php or ask your own question.