php 使用数组上传文件问题

php upload file issue using array

在我的表单中,我尝试使用 php 上传多个或单个文件。所以我的 html table 是这样的:

<tr>
<td width="142"><b>Docs</b>
<td width="142"><input type="file" name="files[]" id="project_docs1" class="docfile" /></td>
<td width="142"><input type="file" name="files[]" id="project_docs2" class="docfile" /></td>
<td width="142"><input type="file" name="files[]" id="project_docs3" class="docfile" /></td>
</td>

现在,当我只上传一个文件时,它会显示如下错误消息:格式无效,但它应该接受一个文件。不需要上传所有 3 个文件。你能告诉我为什么它会显示这条名为 Invalid Format 的错误消息吗?如果上传所有 3 个文件,那么它工作正常。

当我在没有上传任何文件的情况下按下上传按钮时,它显示 $noOfUpload 变量的值为 1。为什么?

$valid_formats = array("jpg", "png", "gif", "txt", "bmp");
$max_file_size = 1024*100; //100 kb
$path = "project_docs"; // Upload directory
$error =  array();  // load error message
$files_name =  array(); // get uploaded file name

foreach ($_FILES['files']['name'] as $key => $name) {

    $size = $_FILES['files']['size'][$key]. "<br>"; 
    $noOfUpload = count($name);

    if($noOfUpload <= 0){
        $error[] =  "Upload your document/s<br>";
    }elseif(!in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)){
        $error[] = "invalid file formate : $name<br>";

    }

    //$name = md5(uniqid()) . '-' . htmlspecialchars_decode($name);
    $files_name[] = "$name";

}

if(!empty($error)){
    foreach ($error as $e) {
        echo "<div class='error'>$e</div>";
    }   
}else{
    foreach ($files_name as $fn) {  
        echo "$fn<br>";
    }       
}

感谢您的帮助。 :)

您的代码是 运行 整个数组,无论是否为空。首先,计算您的数组,然后 运行 您的代码达到该计数。

试试这个

    if (isset($_FILES))
    {
        $valid_formats = array("jpg", "png", "gif", "txt", "bmp");
        for ($i = 0; $i < count($_FILES['files']['name']); $i++)
        {
           if (in_array(pathinfo($FILES['files']['name'][$i], PATHINFO_EXTENSION), $valid_formats))
          {
             $tmp_path = $_FILES['files']['tmp_name'][$i];
             if ($tmp_path != "")
             {
                 if (move_uploaded_file($tmp_path, $new_path))
                 {
                     //Handle other code here
                 }
                 else
                 {
                     $error[] = ""; //your error handling
                 }
             }
             else
             {
                 $error[] = ""; //your error handling
             }
         }
         else
         {
             $error[] = "invalid file formate : $name<br>";
         }
     }
 }
    <tr>
     <td width="142"><b>Docs</b>
     <td width="142"><input type="file" name="files[]" id="project_docs1" class="docfile" /></td>
    </tr>

PHP代码:

    $valid_formats = array("jpg", "png", "gif", "txt", "bmp");
    $max_file_size = 1024*100; //100 kb
    $path = "project_docs"; // Upload directory
    $error =  array();  // load error message
    $files_name =  array(); // get uploaded file name

    foreach ($_FILES['files']['name'] as $key => $name) {

    $size = $_FILES['files']['size'][$key]. "<br>"; 
    $noOfUpload = count($name);

    if($noOfUpload <= 0){
        $error[] =  "Upload your document/s<br>";
    }elseif(!in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)){
        $error[] = "invalid file format : $name<br>";

    }

    //$name = md5(uniqid()) . '-' . htmlspecialchars_decode($name);
    $files_name[] = "$name";

}

if(!empty($error)){
    foreach ($error as $e) {
        echo "<div class='error'>$e</div>";
    }   
}else{
    foreach ($files_name as $fn) {  
        echo "$fn<br>";
    }       
}