PHP处理多个文件上传很容易

时间:2012-02-01 01:56:02

标签: php upload

已解决! 使用此示例从php.net post方法处理多个上传,使用andre的答案获得的知识,这就是为什么我选择它作为答案,它来了!

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");
}

}

结果是第一张图片上传为45.jpg,第二张图片上传为45-1.jpg等等!谢谢你的帮助:D

3 个答案:

答案 0 :(得分:2)

问题是如何迭代$ _FILES数组。它不像你想象的那样处理多个文件,所以使用下面的函数并将它们合并到你的脚本中。以下脚本的功劳归功于PHP站点上的人。链接在代码下方。

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
                )
        )
)
*/
?>

我从PHP website

中取出了这个

答案 1 :(得分:1)

<?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++;
}
?>

试试。

答案 2 :(得分:1)

只需使用我提供的功能并合并如下:

<?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++;
}
?>