I borrowed this code from another StackOverflow question, yet for some reason I cannot get it to work. When testing it is not even passing the files. My error testing shows
total: 0
So it fails right there. Any help would be greatly appreciated
session_start();
$total = count($_FILES['upload']['name']);
echo "total: " . $total;
for( $i=0 ; $i < $total ; $i++ ) {
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
echo $tempFilePath . "<br>";
if ($tmpFilePath != ""){
$newFilePath = "/images/prod/" . $_FILES['upload']['name'][$i];
echo $newFilePath . "<br>";
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
echo $_FILES['upload']['name'];
}
}
}
<form action="addpics.php" method="get" name="add" enctype="multipart/form-data>
<input type="file" name="imgs[]" multiple>
<input type="submit" value=" - ADD - ">
</form>
Expect the files to upload to the /images/prod folder and echo the array of file names
The issue is because your method, enctype & field name is wrong. As others have mentioned you are missing the quote on enctype.
You are trying to get the upload field from your form but the form doesn't have this field, so you need to update file input and rename it to upload[]
Also you are trying to submit the form via a GET request which wont pass the data across.
This code should fix the issue (I've changed it so it posts to itself rather than your addpics.php
file)
<?php
if(isset($_FILES['upload'])){
session_start();
$total = count($_FILES['upload']['name']);
echo "total: " . $total;
for( $i=0 ; $i < $total ; $i++ ) {
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
echo $tempFilePath . "<br>";
if ($tmpFilePath != ""){
$newFilePath = "/images/prod/" . $_FILES['upload']['name'][$i];
echo $newFilePath . "<br>";
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
echo $_FILES['upload']['name'];
}
}
}}
?>
<form method="post" name="add" enctype="multipart/form-data">
<input type="file" name="upload[]" multiple>
<input type="submit" value=" - ADD - ">
</form>
you didn't close the inverted comma of enctype. enctype="multipart/form-data"