0

I trying to upload array images by using move_upload_file function but getting error

HTML conde

<input type="file" name="gallery_images[]" class="custom-file-input" name="slider_image" placeholder="Upload Image" >

PHP code

foreach ($_FILES['gallery_images']["name"] as $row=>$name){
                $gallery_images = $name;                                
                $images_content = explode(".", $gallery_images);
                $gallery_imagename = round(microtime(true)) . '.' . end($images_content);               
                move_uploaded_file($_FILES["gallery_images"]["tmp_name"], "./ThumbnailImage/" . $gallery_imagename);

         }  

Error

Warning: move_uploaded_file() expects parameter 1 to be string, array given in

What is the wrong in code?

IncredibleHat
  • 4,000
  • 4
  • 15
  • 27
Riya
  • 11
  • 1
    "*What is the wrong in code?*" ... this is what: "*move_uploaded_file() expects parameter 1 to be string, array given*" – IncredibleHat Jul 07 '18 at 14:01
  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – James Jul 07 '18 at 14:05
  • Two parameter add in function anything missing regarding this – Riya Jul 07 '18 at 14:05
  • `move_uploaded_file()` needs the first parameter to be a string but you give it an array. Don't give it an array, give it a string. There's nothing else we can help with really, you just need to make sure you provide the correct data to the function. The error message cannot be better, it literally tells you what is wrong :) – James Jul 07 '18 at 14:07
  • 1
    You should have a look [at the manual about uploading multiple files](http://php.net/manual/en/features.file-upload.multiple.php). If you only upload _one_ file (and not ending the input name with `[]`) then `$_FILES["gallery_images"]["tmp_name"]` would be a string. Otherwise, it will contain an array with all the temp names. – M. Eriksson Jul 07 '18 at 14:08
  • All `$_FILES["gallery_images"][...]` are going to be arrays due to using `[]` in the form element name. So when you use `$_FILES["gallery_images"]["tmp_name"]`, its an array. Not a single value. You need `$_FILES["gallery_images"]["tmp_name"][$row]` based on your foreach definition. ... haha... stealth edit preempted me! – IncredibleHat Jul 07 '18 at 14:08
  • you need to learn about how loops work in php. – Rotimi Jul 07 '18 at 14:09
  • Props on using a fairly unique custom-made filename (instead of using what comes in from the user's computer). However microtime *may* still cause some clashes in that loop (maybe rare due to the loops execution time). – IncredibleHat Jul 07 '18 at 14:16
  • @ IncredibleHat $_FILES["gallery_images"]["tmp_name"][$row] this code is working fine thanks :) – Riya Jul 07 '18 at 14:19

1 Answers1

1

Because you have named the form element gallery_images[], with the square brackets, the form will send an array, instead of a single element, to the server. You should name it gallery_images, unless you offcourse want to upload multiple files at once.

Because you post an array, $_FILES["gallery_images"]["tmp_name"] gonna be an array to. This array will contain all the temporary names of the uploaded files.

To upload multiple images you need to select the index in the $_FILES["gallery_images"]["tmp_name"] array. You already have the key ($row) in the foreach loop, so it's as easy as $_FILES["gallery_images"]["tmp_name"][$row]

Next time you could check what is in the array. Just var_dump($_FILES["gallery_images"]["tmp_name"]) to see how the array is structured. To pretty-print it for readability, you could put in inside <pre></pre> tags. You can also use print_r instead of var_dump.

Ramon Bakker
  • 1,075
  • 11
  • 24