简体   繁体   中英

PHP get names of multiple uploaded files

Hi im trying to create a script to upload multiple files. everythings works so far and all files are being uploaded.

My problem now is that i need to grap each file name of the uploaded files, so i can insert them in to a datebase.

Here is my html:

<form action="" method="post" enctype="multipart/form-data">

  <input type="file" name="image[]" multiple />
  <input type="submit" value="upload" />

And here is the php:

if (!empty($_FILES['image'])) { 
    $files = $_FILES['image'];

    for($x = 0; $x < count($files['name']); $x++) {
        $name       = $files['name'][$x];
        $tmp_name   = $files['tmp_name'][$x]; 
        $size       = $files['size'][$x]; 

        if (move_uploaded_file($tmp_name, "../folder/" .$name)) {
            echo $name, ' <progress id="bar" value="100" max="100"> </progress> done <br />';
        }
    }
}

Really hope someone can help me with this. /JL

You have your indexes backwards. $_FILES is an array, with an element for each uploaded file. Each of these elements is an associative array with the info for that file. So count($files['name']) should be count($files) , and $files['name'][$x] should be $files[$x]['name'] (and similarly for the other attributes. You can also use a foreach loop:

foreach ($_FILES as $file) {
  $name       = basename($file['name']);
  $tmp_name   = $file['tmp_name'];
  $size       = $file['size'];

  if (move_uploaded_file($tmp_name, "../folder/" .$name)) {
      echo $name, ' <progress id="bar" value="100" max="100"> </progress> done <br />';
  }
}

I think the glob() function can help:

<?php
    foreach(glob("*.jpg") as $picture)
    {
        echo $picture.'<br />';
    }
?>

demo result:

pic1.jpg
pic2.jpg
pic3.jpg
pic4.jpg
...

be sure to change the file path and extension(s) where necessary.

Thanks for the help, i got it to work by simply creating variables:

$image_1 = (print_r($files['name'][0], true)); 
$image_2 = (print_r($files['name'][1], true)); 
$image_3 = (print_r($files['name'][2], true)); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM