Hi if you want to upload two separate files from one single form to be uploaded and with a for simplest code
Look at mine code.
The form will have two separate input files but you need to name them common, ex 'files[]' I'm using 'upload[]' in this case.
<html>
<body>
<form method="post" action='insert.php' enctype='multipart/form-data'>
<input name="upload[]" type="file" />
<input name="upload[]" type="file" />
<input type="submit" value="submit">
</form>
</body>
</html>
And the insert.php file will be gone like this, Since you have two files you will loop the code for <2, You can also use the count function.
<?php
for($i=0; $i<2; $i++) {
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
if ($tmpFilePath != ""){
$file[$i] = $newFilePath = "upload/" . time().$_FILES['upload']['name'][$i];
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
}
}
}
?>
$file[$i] = $newFilePath = "upload/" . time().$_FILES['upload']['name'][$i];
In the above line I concatenate time() function with file name so that each file will generated name different with time. and upload it the folder name where files will be uploaded.
0 Comment(s)