How to upload multiple images in PHP and store in Mysql

Multiple images uploading option is very handy if you want to upload more than one image at a time. Because multiple images upload option save lots of time of the user.

In this tutorial I am going to show you how to upload multiple images in PHP and store in mysql. I am going to create dynamic input fields using jquery and save them in mysql table.

How Upload multiple Images in PHP works:

  • First I will create a html form with one file input field.
  • Second i will add jquery code to create dynamic file input fields.
  • Third write PHP code for file type validation, image size restriction and move images to target folder.
  • Four if image successfully transfer to target folder then i will save image name in the database.

Also read:


Create Mysql Database:

Create Mysql Table:

Upload Form HTML:

File upload form must have enctype=”multipart/form-data” attritube.

jQuery Code:

PHP Code:

$targetFolder holds the “Uploads” directory name. $errorMsg and $successMsg are arrays of error and success messages. Then I started loop on $_FILES and checked $fileArray['name'] and $fileArray['error'] must not be empty and zero. $getFileExtension holds the extension of images. If user upload any file other than image code will print an error. Then i checked size of image which should not more than 500kb. $breakImgName is used to break the image name. $newFileName is used to create new image name by using strotime and str_replace function. $targetPath variable holds the new image path. move_uploaded_file function is used to move image from temporary storage to the targeted path. If image moved properly then image name is saving to the images table.

All upload multiple images Code together

Also read:

 

Posted in PHP

Author: Ahsan Zameer

I am a web developer and love to search new stuff on web. Trying different approaches and ideas to make web developing more interesting and enjoyable.