-3

我有一个上传脚本来上传一些文件到一个目录。每个文件都通过一个循环运行,并且将检查大小或结尾是否存在错误。如果没有错误,它将被上传。

if (is_array($_FILES ['image'] ['tmp_name'])) {
    foreach ( $_FILES ['image'] ['tmp_name'] as $key => $val ) {
        ...

        if (! in_array ( $fileExt, $allowedExtensions )) {
            $errors [$fileName] [] = "format not accepted";
        }...

            if ((count ( $errors1 ) == 0) && (count ( $errors ) === 0))  {
               if (move_uploaded_file ( $fileTemp, $fileDst )) {
                //...                               
            }
        }   
     }
}

我的问题是,有没有办法计算成功通过该循环的上传文件的数量?多谢。

4

2 回答 2

2

您需要计算每次成功的上传。

如下所示:

   if (is_array($_FILES ['image'] ['tmp_name'])) {
    $Counter=0;     // initialize counter variable
        foreach ( $_FILES ['image'] ['tmp_name'] as $key => $val ) {

            $fileName = $_FILES ['image'] ['name'] [$key];
            $fileSize = $_FILES ['image'] ['size'] [$key];
            $fileTemp = $_FILES ['image'] ['tmp_name'] [$key];

            $fileExt = pathinfo ( $fileName, PATHINFO_EXTENSION );
            $fileExt = strtolower ( $fileExt );

            if (empty ( $fileName ))
            continue;

            if (! in_array ( $fileExt, $allowedExtensions )) {
                $errors [$fileName] [] = "format not accepted";
            }...

                if ((count ( $errors1 ) == 0) && (count ( $errors ) === 0))  {
                   if (move_uploaded_file ( $fileTemp, $fileDst )) {
                    //...           
                   $Counter++;       // increment if successful upload
                }
            }   
         }
    }

echo $Counter;  //it will give total count of successfully uploaded files
于 2012-04-19T17:10:07.507 回答
1

只需使用计数器变量。move_uploaded_file我了解您在返回 true时已成功上传文件,对吗?

$counter = 0;
//... your code
if ((count ( $errors1 ) == 0) && (count ( $errors ) === 0))  {
    if (move_uploaded_file ( $fileTemp, $fileDst )) {
        $counter++;
        //... some other code
    }
}

所以,当你离开foreach循环$counter时就会有预期的值。

于 2012-04-19T17:11:51.080 回答