Single/multiple image validation and upload in PHP


In my last snippet I have explained how to validate single/multiple image field using JavaScript, but the rule of thumb is “NEVER TRUST USER”. User can try to upload malicious file using different techniques and javaScript validation is not secure. So what we do, we validate the image both at client and server side. In this tutorial I am going to explain how to validate Single/multiple image field and and upload in PHP.

Validate and upload single image in PHP

I am going to create a file called index.php where I am going to code and test my image validation code. First of all I am going to create a form with a file field and a submit button and upon form submission I am going to validate the file if its an image or not and upon validation I will upload it to the server. Also create a folder called “uploads” (you can give whatever name you want) on your server and make sure you have write permission to that folder.

Check the html form code below

Once the html form is created just write the php code above the form code ( strongly recommended). I am validating image of type jpg, gif,png and bmp.

Once you are done with this tutorial have a look at these posts as well, it will definitely help you.

Validate and upload multiple images in PHP

Once you know how validation and upload code for single image works, uploading multiple image will just be a piece of cake. The primary difference when validating between single and multiple image fields is that the single image is a array whereas the multiple image field is an array of arrays. Have a look at their internal structure when I upload single image vs multiple images.

As you have seen, during multiple file upload you get an array of array of file field. Now let me code how to validate and upload multiple file field. Let me first start with creating a form that will accept multiple files. Please note that the name of the file field is “f[]” which represent an array.

Once the user attach multiple file and submit the form, it will validate and upload the image file on the server. Have a look at the PHP code below.

You can similarly validate other files extension like PDF, excel, word and many more, I have already posted a list of common mime type here.

If you guys have some suggestion/feedback please share with us.


About Author

Entrepreneur & Dreamer

I am a passionate Software Professional, love to learn and share my knowledge with others.

Software is the hardware of my life.


  1. please help me beginning the stage in php developing send me a sample validation code for signup page and i need image validation code

Leave A Reply

CommentLuv badge

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.