PHP多文件上传(数组)问题

时间:2014-06-13 06:53:07

标签: php file-upload

我使用foreach循环上传多个文件。但我的文件没有移动到文件夹, 这是我的代码。

foreach ($_FILES['image']['name'] as $file)
    {
            print_r($file);
            echo "<br>";
            $errors= array();
            $file_name = $_FILES['image']['name'];
            $file_size =$_FILES['image']['size'];
            $file_tmp =$_FILES['image']['tmp_name'];
            $file_type=$_FILES['image']['type']; 

            echo $file = $baseMODULES."/upload/".$_FILES['image']['name'];  
            echo "<br><br>";
            $clogo = $_FILES['image']['name'];
            move_uploaded_file($_FILES['image']['tmp_name'],$file);     

            //File Loading Successfully

    }

这是输出。

enter image description here

2 个答案:

答案 0 :(得分:2)

<?php    
$i = 0 ;   
    foreach ($_FILES['image']['name'] as $file)
            {
                    print_r($file);
                    echo "<br>";
                    $errors= array();
                    $file_name = $_FILES['image']['name'][$i];
                    $file_size =$_FILES['image']['size'][$i];
                    $file_tmp =$_FILES['image']['tmp_name'][$i];
                    $file_type=$_FILES['image']['type'][$i]; 

                    echo $file = $baseMODULES."/upload/".$_FILES['image']['name'][$i];  
                    echo "<br><br>";
                    $clogo = $_FILES['image']['name'][$i];
                    move_uploaded_file($_FILES['image']['tmp_name'][$i],$file);     

                    //File Loading Successfully
               $i++;
            }
?>

试试这个

<?php    
$i = 0;
    foreach ($_FILES['image']['name'] as $key=>$file)
            {
                    print_r($file);
                    echo "<br>";
                    $errors= array();
                    $file_name = $_FILES['image']['name'][$key];
                    $file_size =$_FILES['image']['size'][$key];
                    $file_tmp =$_FILES['image']['tmp_name'][$key];
                    $file_type=$_FILES['image']['type'][$key]; 

                    echo $file = $baseMODULES."/upload/".$_FILES['image']['name'][$key];  
                    echo "<br><br>";
                    $clogo = $_FILES['image']['name'][$key];
                    move_uploaded_file($_FILES['image']['tmp_name'][$key],$file);     

                    //File Loading Successfully
               $i++;
            }
?>

答案 1 :(得分:1)

您应该将foreach循环更改为以下内容:

foreach($_FILES['image'] as $image) {

然后用

之类的东西进行移动
move_uploaded_file($image['tmp_name'], $file);

如评论中所述: - )