Saturday, January 29, 2011

Multiple File Upload with PHP

Use following code to provide a functionality to upload multiple files from a single form.

Create HTML form as mentioned in the code below:

<html>
<body>
<form action="addPhotoAction.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="rowCount" id="rowCount" value="2">
<table>
<tr>
<td>Photo 1</td>
<td><input type="file" name="photo0" id="photo0"></td>
</tr>
<tr>
<td>Photo 2</td>
<td><input type="file" name="photo1" id="photo1"></td>
</tr>
</table>
</form>
</body>
</html>



Copy & paste following code to create addActionPhoto.php:


<html>
<body>
<?php
$rowCount=$_POST[‘rowCount’];
for($i = 0; $i < $rowCount; $i++) // where rowCount is the No. of uploads
{
$target_path = "FolderName_to_store_images/";
$target_path = $target_path . basename( $_FILES[photo'. $i]['name']);

$file_name = stripslashes($target_path);
$file_name = str_replace("'","",$file_name);
$copy = move_uploaded_file($_FILES[photo'. $i]['tmp_name'],$file_name);

// prompt if successfully copied
if($copy!="")
{
//here comes the insert query
echo “Images uploaded successfully.<br>”;
}
else
{
echo "$file_name | could not be uploaded!<br>";
}
}
?>
</body>
</html>


NAVSYA Technologies Pvt. Ltd.
www.navsya.com

No comments:

Post a Comment