Reputation: 63
This is my second question on the forum. I've exhausted all avenues of researching this on my own. I have an HTML form the will be processed with a script. In this form the user has the option to upload up to 10 images.... each image has its own entry field like this...
<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
The php is this...
$client = $_POST['company'];
$date = date("mdy");
$clientFolder = $client . $date;
mkdir('../../../uploads/' . $clientFolder . '/', 0700);
$folderPath = '../../../uploads/' . $clientFolder . '/';
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 100000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists($folderPath . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
$folderPath . $_FILES["file"]["name"]);
echo "Stored in: " . $folderPath . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
I can get one file to upload correctly but not more than one. I used this tutorial. http://www.w3schools.com/php/php_file_upload.asp
Do I need to loop through these? Or do I need unique name's and id's? Any help will be appreciated! I'm new to php.... I have to say though.. I love it!!! so far...
Upvotes: 0
Views: 792
Reputation: 6612
Maybe you want to name your inputs differently (say: file1, file2 etc) then in PHP you would do something like:
$i = 1;
while(isset($_POST['file'.$i])){
//do upload here
$i++;
}
The reason why you get only 1 file uploaded is because you named the inputs "file" (and because you don't loop through the inputs). Change that and it should be fine.
Upvotes: 0
Reputation: 146603
I haven't browsed your full code but if you use the same name for different form elements you will lose all values except one.
There's a little exception that you'll probably want to use: adding square brackets will make PHP build an array:
<label for="file">Filename:</label>
<input type="file" name="file[]">
<label for="file">Filename:</label>
<input type="file" name="file[]">
You can use var_dump()
to inspect the exact structure from $_FILES
.
Secondly, the id
HTML attribute is supposed to hold a unique identifier. Your client-side scripting is likely to behave wrong.
Upvotes: 1