简体   繁体   English

PHP处理多个文件上传轻松

[英]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! 使用来自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");
}

} }

The result is the first image is uploaded as 45.jpg, the second is uploaded as 45-1.jpg and so forth! 结果是第一个图像以45.jpg上传,第二个图像以45-1.jpg上传,依此类推! Thanks for the help guys :D 谢谢你们的帮助:D

The problem is how you are iterating over the $_FILES array. 问题是您如何遍历$ _FILES数组。 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. 以下脚本的功劳归于PHP网站上的家伙。 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网站上获取的

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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