0

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...

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This is interesting... I'm eager to learn as much as I can. I went over to the php manual to check our var_dump() but couldn't wrap my brain around this. Would you give me another hint on what to do next?
The next is step is probably the Handling file uploads chapter.
0

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.

3 Comments

OK so I'm trying to think this through. I would have <label for="file1">Filename:</label> <input type="file" name="file1" id="file1" /> <label for="file2">Filename:</label> <input type="file2" name="file" id="file2" /> THEN $i = 0; while(isset($_POST['file'.$i])){ //do upload here --- ???? the code that goes here starts if ((($_FILES["file"]["type"] == "image/gif") and includes whole statement???? //// $i++; }
exactly, include the whole if statement
sorry, had a bug, $i should start from 1, not 0, if you are naming the inputs from file1. i've edited my post appropriately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.