Download multiple files using html5 and php

I have a file upload form configured using the HTML5 multiple attribute.

However, the form saves only one file. Do I need to create some kind of loop function in php or is there any other way to do this?

Here is my code ...

the form:

<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data"> <input type="file" multiple="multiple" name="file[]" id="file" /> <input name="submit" type="submit" value="Submit" /> </form> 

PHP:

 <?php if(isset($_POST['submit'])) { foreach($_FILES['newsImage'] as $file){ if ((($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg"))) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } } } ?> 
+4
source share
3 answers
 for ($i = 0; $i < count($_FILES['newsImage']['name']); $i++) { // handle upload } 
+3
source

I believe your field should be <input type="file" multiple="multiple" name="files[]" />

And then in PHP:

 <?php foreach($_FILES['files'] as $file){ // Handle one of the uploads } ?> 
+3
source

I believe this code can serve a purpose. It goes through the $_FILES and creates an array with key => value pair of all attributes for each file.

 $temp = array(); foreach ($_FILES['file'] as $key => $value) { foreach($value as $index => $val){ $temp[$index][$key] = $val; } } 
+2
source

Source: https://habr.com/ru/post/1389253/


All Articles