There is such a code:

<?php foreach ($_POST as $index => $value) { $$index = $value; } $uploaddir = './files/'; for ($i=0; $i<=count($_FILES); $i++) { $uploadFile = $uploadDir.basename($_FILES['userFile']['name'][$i]); if (copy($_FILES['userFile']['tmp_name'][$i], $uploadFile)) { echo "<h3>Файл успешно загружен на сервер</h3>"; } else { echo "<h3>Ошибка! Не удалось загрузить файл на сервер!</h3>";} } ?> <form action="mce.php" method="post" enctype="multipart/form-data"> <input name="userFile[]" type="file" multiple><br> <input type="submit" value="Загрузить"> </form> 

In theory, it should load many files, but does not load more than two files. I understand that there is some kind of mistake in the cycle, but I can’t figure out how to fix it. What's wrong?

  • one
    Well, where is $_POST ? - Raz Galstyan
  • one
    What do you call input'y? - Jean-Claude
  • the error is not in the loop, but in how you drew the html form. Show it. - AK
  • Added a form code - ind

1 answer 1

In your example, count($_FILES) will always be 1, since $_FILES['userfile'] will contain a subarray of information about several files, and not $_FILES .

Also an error in this line is $i < count($_FILES['userFile']['name'] , you had <= , but you just have to put a less sign, because the report is zero.

Well, use move_uploaded_file, at least the developers went this way.

 <form action="uploadfiles.php" enctype="multipart/form-data" method="post"> <input type="file" name="userFile[]" multiple> <input type="submit" name="submit"> </form> <?php if (isset($_POST['submit'])) { $uploaddir = __DIR__ . '/files/'; for ($i = 0; $i < count($_FILES['userFile']['name']); $i++) { $uploadFile = $uploaddir . basename($_FILES['userFile']['name'][$i]); echo $uploaddir . basename($_FILES['userFile']['name'][$i]); if (move_uploaded_file($_FILES['userFile']['tmp_name'][$i], $uploadFile)) { echo "<h3>Файл успешно загружен на сервер</h3>"; } else { echo "<h3>Ошибка! Не удалось загрузить файл на сервер!</h3>"; } } }