Taking too long? Close loading screen.
Opening Up A New Dimension

PHP Multiple file Uploading

Posted by: Herald Lynx Comments: 0 0 Post Date: May 26, 2016

Notice using the exact code is not advised!

This tutorial covers PHP multiple file uploading. Uploading multiple files can be tricky for beginners. Well you can look at the sample code here first make a file and name it as “upload.php” place the following code in it and modify it for your needs like path, file name, html input names, you can insert the record in the after the file is moved…

if(isset($_FILES['uploadFile']))
{
$countarray = count($_FILES['uploadFile']['name']);
$newarray = array();
for($i=0;$i<$countarray;$i++)
{
$image_array[$i]['name']=$_FILES['uploadFile']['name'][$i];
$image_array[$i]['type']=$_FILES['uploadFile']['type'][$i];
$image_array[$i]['tmp_name']=$_FILES['uploadFile']['tmp_name'][$i];
$image_array[$i]['error']=$_FILES['uploadFile']['error'][$i];
$image_array[$i]['size']=$_FILES['uploadFile']['size'][$i];
$allowed = array ('image/pjpeg', 'image/jpeg', 'image/jpeg', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png');
$uploadPath = 'uploads/';
$file_name = $image_array[$i]['name'];
$target = $uploadPath.$file_name;
if (in_array($image_array[$i]['type'], $allowed))
{

if (move_uploaded_file($image_array[$i]['tmp_name'], $target))
{
//insert record in db
}//END of move_uploaded_file
else
{
$msg[] = 'A problem occured while uploading. <b>Please try again.</b>';
}

}// END of in_array
}// End of For Each

}// End of if isset

<table style="width:100%;">
<form method="post" action="upload.php" enctype="multipart/form-data">
<tbody>
<tr><td>File 1</td><td><input type="file" name="uploadFile[]"></td></tr>
<tr><td>File 2</td><td><input type="file" name="uploadFile[]"></td></tr>
<tr><td>File 3</td><td><input type="file" name="uploadFile[]"></td></tr>
<tr><td>File 4</td><td><input type="file" name="uploadFile[]"></td></tr>
<tr><td>File 5</td><td><input type="file" name="uploadFile[]"></td></tr>
<tr><td colspan="2" style="text-align:right;"><input type="submit" name="submit" value="Submit"></td></tr>

</tbody>
</form>
</table>

 

Share this post

Leave a Reply