How to do multiple file upload using a single HTML file control using PHP

In cases where you are uploading multiple images against a single product it would be best and more user friendly to allow the user to select all the images in one go rather than going through each control and continuing it after uploading the first batch of files.

Being a programmer you can make this much better. How about decreasing the number of HTML file controls from 5 to 1? ?It’s possible in HTML and if you are coding in PHP you will be able to upload the files just fine.

To allow multiple file upload you will need to make the following change to the HTML file control

            <form method="post" enctype="multipart/form-data">
                <input type="file"name="file_img[]" multiple  /> File 
            </form>
$numVals = count($_FILES['file_img']['name']);
$path = "product_images/";
										
for($jj=0; $jj < $numVals; $jj++) {
									
    if(move_uploaded_file($_FILES["file_img"]["tmp_name"][$jj], $path.$_FILES["file_img"]["name"][$jj]))
    {
    	// Write code here
	}
	else {
        // In case error during file upload
		print_r($_FILES["file_img"]["error"][$jj]);
	}

}

The beauty of this is that you can upload as many files as you want provided you do not go above the upload_max_filesize setting.

Hope the above helped. Let me know if you need code to help you get along.

Please follow and like us:

Be the first to comment

Leave a Reply

Your email address will not be published.


*