PHP Multiple File Array
you can use my updated code and as per my demo it is working perfect for multiple file upload
<?php
if(isset($_FILES['documents'])){
foreach($_FILES['documents']['tmp_name'] as $key => $tmp_name)
{
$file_name = $key.$_FILES['documents']['name'][$key];
$file_size =$_FILES['documents']['size'][$key];
$file_tmp =$_FILES['documents']['tmp_name'][$key];
$file_type=$_FILES['documents']['type'][$key];
move_uploaded_file($file_tmp,"galleries/".time().$file_name);
}
}else{
echo "<form enctype='multipart/form-data' action='test1.php' method='POST'>";
echo "File:<input name='documents[]' multiple='multiple' type='file'/><input type='submit' value='Upload'/>";
echo "</form>";
}
?>
For anyone trying to do it with a single file php function (i`m using classes, but you can change to a function):
html:
<input type="file" name="foto[]" />
<input type="file" name="foto[]" />
<input type="file" name="foto[]" />
<input type="file" name="foto[]" />
<input type="file" name="foto[]" />
php:
if (isset($_FILES['foto'])) {
$arquivo = array();
foreach ($_FILES['foto']["name"] as $file=>$key) {
// the empty input files create an array index too, so we need to
// check if the name exits. It means the file exists.
if (!empty($_FILES['foto']["name"][$file])) {
$arquivo ["name"] = $_FILES['foto']["name"][$file];
$arquivo ["type"] = $_FILES['foto']["type"][$file];
$arquivo ["tmp_name"] = $_FILES['foto']["tmp_name"][$file];
$arquivo ["error"] = $_FILES['foto']["error"][$file];
$arquivo ["size"] = $_FILES['foto']["size"][$file];
$foto = new foto(); // create an obj foto
// $arquivo means file, it`s our file format as a single $_file['file']
if ($foto -> upload($arquivo)) { // if its uploaded than save
$foto -> save();
}
}
}
}
on my foto class:
public function upload($foto) {
$upload_dir = "D:/xampp/htdocs/prova/fotos/";
$file_dir = $upload_dir . $foto["name"];
$move = move_uploaded_file($foto["tmp_name"], $file_dir);
$this -> arquivo = $foto["name"]; // use this to save to db later
// this serves to return true if the file is uploaded
$retorno = ($move) ? 1 : 0;
return $retorno;
}