2

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

4

3 回答 3

4

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 />';
  }
}
于 2013-04-17T20:46:10.553 回答
1

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.

于 2013-04-17T20:54:02.513 回答
1

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)); 
于 2013-04-18T07:26:08.950 回答