简体   繁体   中英

PHP Handle Multiple File Uploads EASY

SOLVED! Used this example from php.net post method handling multiple uploads, with the knowledge gained by using andre's answer which is why I selected it as the answer, here it goes!

foreach ($_FILES["userfile"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
  if ($x=="0"){
    $data=explode(".",$_FILES["userfile"]["name"][$key]);
    $named=$id[id].".".$data[1];
    $x=$x+1;
    }else{
    $data=explode(".",$_FILES["userfile"]["name"][$key]);
    $named=$id[id]."-".$x.".".$data[1];
    $x=$x+1;
    }
    $uploaddir = '/home/content/92/8498392/html/items/'.$_POST[catid];
    $tmp_name = $_FILES["userfile"]["tmp_name"][$key];
    move_uploaded_file($tmp_name, "$uploaddir/$named");
}

}

The result is the first image is uploaded as 45.jpg, the second is uploaded as 45-1.jpg and so forth! Thanks for the help guys :D

The problem is how you are iterating over the $_FILES array. It does not handle mutliple files like you think so use the functions below and incorporate them into your script. The credit for the script below goes to the guy on the PHP site. Link is below the code.

function multiple(array $_files, $top = TRUE)
{
    $files = array();
    foreach($_files as $name=>$file){
        if($top) $sub_name = $file['name'];
        else    $sub_name = $name;

        if(is_array($sub_name)){
            foreach(array_keys($sub_name) as $key){
                $files[$name][$key] = array(
                    'name'     => $file['name'][$key],
                    'type'     => $file['type'][$key],
                    'tmp_name' => $file['tmp_name'][$key],
                    'error'    => $file['error'][$key],
                    'size'     => $file['size'][$key],
                );
                $files[$name] = multiple($files[$name], FALSE);
            }
        }else{
            $files[$name] = $file;
        }
    }
    return $files;
}

print_r($_FILES);
/*
Array
(
    [image] => Array
        (
            [name] => Array
                (
                    [0] => 400.png
                )
            [type] => Array
                (
                    [0] => image/png
                )
            [tmp_name] => Array
                (
                    [0] => /tmp/php5Wx0aJ
                )
            [error] => Array
                (
                    [0] => 0
                )
            [size] => Array
                (
                    [0] => 15726
                )
        )
)
*/
$files = multiple($_FILES);
print_r($files);
/*
Array
(
    [image] => Array
        (
            [0] => Array
                (
                    [name] => 400.png
                    [type] => image/png
                    [tmp_name] => /tmp/php5Wx0aJ
                    [error] => 0
                    [size] => 15726
                )
        )
)
*/
?>

I took this from the PHP website

<?php
$id = 877;
$x = 0;
foreach ($_FILES as $file) {
  $data = explode(".", $file['userfile']['name']);
  $fileExtension = $data[count($data)-1];
  if ($x == 0) {
    $filePath = '/home/content/92/8498392/html/items/' . $_POST['catid'] . '/' . $id . '.' . $fileExtension; 
  }
  else {
    $filePath = '/home/content/92/8498392/html/items/'.$_POST['catid'].'/' . $id . '-' . $x . '.' . $fileExtension;
  }
  move_uploaded_file($file['userfile']['tmp_name'], $filePath);
  $x++;
}
?>

Try that.

Just use the function I supplied and incorporate like below:

<?php
$id = 877;
$x = 0;
$_FILES = multiple($_FILES); // taken from above snippet from php.net
foreach ($_FILES as $fileKey => $file) {
  $data = explode(".", $file['name']);
  $fileExtension = $data[count($data)-1];
  if ($x == 0) {
    $filePath = '/home/content/92/8498392/html/items/' . $_POST['catid'] . '/' . $id . '.' . $fileExtension; 
  }
  else {
    $filePath = '/home/content/92/8498392/html/items/'.$_POST['catid'].'/' . $id . '-' . $x . '.' . $fileExtension;
  }
  move_uploaded_file($file['tmp_name'], $filePath);
  $x++;
}
?>

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