JSW189
JSW189

Reputation: 6325

Limiting file types/extensions when uploading files in an HTML form

Is there any way to limit the type of file that can be uploaded using an HTML form via PHP? For example, I want to limit uploads to only .pdf files.

Upvotes: 0

Views: 111

Answers (2)

Igoris
Igoris

Reputation: 1650

Server side

Yes use superglobal $_FILES. look at this documentation page : PHP.net

It will get through basics, and then just look for file type, which is what you need to check with.

Client side

There are many ways to do this, but you can achieve this simply :

if((document.form1.upload.value.lastIndexOf(".jpg")==-1) {
   alert("Please upload only .jpg extention file");
   return false;
}

Upvotes: 2

Naftali
Naftali

Reputation: 146340

Yes. just look in the $_FILES array and read the file type.

Upvotes: 1

Related Questions