Multiple file upload – PHP

<script type=”text/javascript”>

function addfileImages() {

document.getElementById(“totalfileImages”).value = Number(document.getElementById(“totalfileImages”).value) + Number(1);

if(Number(document.getElementById(“totalfileImages”).value)>Number(10)){ document.getElementById(“addButton”).style.display=”none”; document.getElementById(“totalfileImages”).value=10; }

document.getElementById(“fileImages”+document.getElementById(“totalfileImages”).value).style.display=”block”;

}

function removefileImages() {

document.getElementById(“fileImages”+document.getElementById(“totalfileImages”).value).style.display=”none”; document.getElementById(“totalfileImages”).value = Number(document.getElementById(“totalfileImages”).value) – Number(1);

if(Number(document.getElementById(“totalfileImages”).value)<Number(10)) document.getElementById(“addButton”).style.display=”block”;

}

</script>

<?php

if(isset($_POST[‘upload’])) {

extract($_POST);

for($i=1;$i<=$totalfileImages;$i++) {

if($_FILES[‘file_image’.$i][‘name’]!=””){

$ext =array_pop(explode(“.”,$_FILES[‘file_image’.$i][‘name’]));

$card_image =”uploads/”.substr(md5(uniqid(rand(),1)),0,32).”.”.$ext;

move_uploaded_file($_FILES[‘file_image’.$i][‘tmp_name’],$card_image) or die(“Could not upload!! make sure that uploads/is writable”);

chmod($card_image,0777) or die(“Could not change permission”);

$success=5;

}else{

$errorList[0]=”Upload all the files “;

} } }?>

<form action=”#” method=”post” enctype=”multipart/form-data”>

<table>

<?php if(count($errorList)>0){ ?>

<tr> <td>&nbsp;</td> <td><div style=”margin:5px; color:#FF0000;”><?php foreach($errorList as $value) echo $value.” ,”; ?></div></td> </tr>

<?php } ?>

<?php if($success==5){ ?>

<tr> <td>&nbsp;</td> <td><div style=”margin:10px; color: #006633; width:90%; font-weight:bold”>File has been uploaded</div></td> </tr>

<?php } ?>

<tr> <td valign=”top”>Select file</td> <td> <?php $maxFiles=10; ?> <div style=”width:99%”> <input type=”file” name=”file_image1″ style=”width:258px;” /></div>

<?php for($i=2;$i<=$maxFiles;$i++) { ?>

<div style=”width:99%; display:none” id=”fileImages<?= $i ?>”>   <input type=”file” name=”file_image<?= $i ?>”  /><a href=”javascript:removefileImages();”>Delete</a> </div>

<?php } ?>

<div align=”right” id=”addButton”><a href=”javascript:addfileImages()”  >Add more</a></div>

<input type=”hidden” id=”totalfileImages”  name=”totalfileImages” value=”1″ /> </td>

</tr>

<tr> <td>&nbsp;</td> <td><input name=”upload” type=”submit”  value=”   Upload   ”  /> </td> </tr>

</table>

</form>

Share

Leave a Reply

Your email address will not be published. Required fields are marked *