dszdiavv474681 2016-04-20 02:51 采纳率: 0%
浏览 49

计算上传文件的数量

I want to count number of uploaded file but I'm unable to get the error if no file had been uploaded. Here is my code for reference:

html

<input type="file" name="file[]" class="filestyle" data-buttonText="Quotation 1" multiple="multiple">

php

$total = count($_FILES['file']['name']);
if($total > '2'){
    for($i=0; $i<$total; $i++){
        $tmpFilePath = $_FILES['file']['tmp_name'][$i];
        if($tmpFilePath != ""){
           $shortname = $_FILES['file']['name'][$i];
           $filePath = "uploads/" . date('d-m-Y-H-i-s').'-'.$_FILES['file']['name'][$i];
           if(!$msgError && move_uploaded_file($tmpFilePath, $filePath)){
                // insert to db and success msg 
        }
    }
} elseif($total < '4') {    
    $msgError[] = "Need to upload 3 Quotations";    
}
if(isset($msgError)){
    $msgErrorString = implode(",",$msgError); 
    header("Location: pr_form.php?msgError=".$msgErrorString."");
}

If user upload less than 3 files, the error will not appear. I have other validations for user input. Everything is working except for the file validation. May I know why?

  • 写回答

1条回答 默认 最新

  • dsh84723 2016-04-20 02:56
    关注

    First: Remove the '

    $total > '2' should be $total > 2

    same with total < 4


    Second:

    It should be

    count($_FILES) not count($_FILES['file']['name'])


    So, in your problem ..

    if no file had been uploaded.

    $total = count($_FILES);
    if($total > 2){
        for($i=0; $i<$total; $i++){
            $tmpFilePath = $_FILES['file']['tmp_name'][$i];
            if($tmpFilePath != ""){
               $shortname = $_FILES['file']['name'][$i];
               $filePath = "uploads/" . date('d-m-Y-H-i-s').'-'.$_FILES['file']['name'][$i];
               if(!$msgError && move_uploaded_file($tmpFilePath, $filePath)){
                    // insert to db and success msg 
            }
        }
    } elseif($total < 4 && $total > 0) {    
        $msgError = "Need to upload 3 Quotations";    
    }
    elseif($total === 0){ //This condition
        $msgError = "No chosen file."; 
    }
    if(isset($msgError)){
        header("Location: pr_form.php?msgError=".$msgError."");
    }
    

    And then in your pr_form.php, you should have something like this line..

    <?php 
       echo $_GET['msgError'];
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法