1
I want to perform multiple uploads but the following message appears:
Notice: Undefined index: files in C: xampp htdocs fotolog Adm upload_fotos.php on line 6
Warning: Invalid argument supplied for foreach() in C: xampp htdocs fotolog Adm upload_fotos.php on line 6
Javascript:
$(document).ready(function(){
$('#uploadForm').on('submit',function(e)){
e.preventDefault();
$.ajax({
url:"upload_fotos.php"
data: new FormData(this),
contentType: false,
processData: function(data)
success: function(data){
$("#galley").html(data);
alert("imagem foi");
}
});
});
});
<div class="container">
<form id="uploadForm" action="upload_fotos.php" method="post">
<div id="gallery"></div><div style="clear:both;"></div><br /><br />
<div class="col-md-4" align="right">
<label>Upload Multiple Image</label>
</div>
<div class="col-md-4">
<input name="files[]" type="file" multiple />
</div>
<div class="col-md-4">
<input type="submit" submitvalue="Submit" />
</div>
<div style="clear:both"></div>
</form>
</div>
</form>
PHP:
<?php
//upload.php
$output = '';
if(is_array($_FILES))
{
foreach ($_FILES['files']['name'] as $name => $value)
{
$file_name = explode(".", $_FILES['files']['name'][$name]);
$allowed_extension = array("jpg", "jpeg", "png", "gif");
if(in_array($file_name[1], $allowed_ext))
{
$new_name = md5(rand()) . '.' . $file_name[1];
$sourcePath = $_FILES['files']['tmp_name'][$name];
$targetPath = "foto/".$new_name;
if(move_uploaded_file($sourcePath, $targetPath))
{
$output .= '<img src="'.$targetPath.'" width="150px" height="180px" />';
}
}
}
echo $output;
}
?>