如何检查至少选择了一个文件进行上传

时间:2014-03-20 14:02:14

标签: php html

如何检查用户是否在下面的代码中选择了至少一个文件进行上传? 我试过in_array,isset,!空函数但没有成功 请注意,userfile输入是html中的数组

if(!empty($_FILES['userfile']['tmp_name'])){
            $upload_dir = strtolower(trim($_POST['name']));
  // Create directory if it does not exist
                if(!is_dir("../photoes/". $upload_dir ."/")) {
                     mkdir("../photoes/". $upload_dir ."/");
                }
            $dirname = "../photoes/".$upload_dir;

                    for($i=0; $i < count($_FILES['userfile']['tmp_name']);$i++)
                        {
            // check if there is a file in the array
                         if(!is_uploaded_file($_FILES['userfile']['tmp_name'][$i]))
                            {
                            $messages[] = 'No file selected for no. '.$i.'field';
                            }
            /*** check if the file is less then the max php.ini size ***/
                        if($_FILES['userfile']['size'][$i] > $upload_max)
                             {
                             $messages[] = "File size exceeds $upload_max php.ini limit";
                             }
            // check the file is less than the maximum file size
                        elseif($_FILES['userfile']['size'][$i] > $max_file_size)
                            {
                            $messages[] = "File size exceeds $max_file_size limit";
                            }
                        else
                            {
                // copy the file to the specified dir 
                            if(@copy($_FILES['userfile']['tmp_name'][$i],$dirname.'/'.$_FILES['userfile']['name'][$i]))
                                {
                    /*** give praise and thanks to the php gods ***/
                                $messages[] = $_FILES['userfile']['name'][$i].' uploaded';
                                }

                            }
                        }
                }else{
                $messages[] = 'No file selected for upload, Please select atleast one file for upload';
                dispform();
            }

3 个答案:

答案 0 :(得分:0)

这是我如何做它的几个if和我使用for循环,因为我允许从一个下拉列表中删除多个文件,但它对你来说更重要

$uploaded = count($_FILES['userfile']['name']);
for ($i=0;$i<$uploaded;$i++) {
    if (strlen($_FILES['userfile']['name'][$i])>1) {
       // file exists so do something
    } else {
      //file doesn't exist so do nothing
    }
}

你会注意到我与全局$ _FILES的name元素进行比较,这是因为你永远不能上传一个没有名字的文件,这也适用于没有上传的文件

不要做客户端这是一个愚蠢的地方进行验证,因为用户可以简单地在浏览器中关闭js处理,或者它可以被某些插件等阻止或通过firebug和各种浏览器搜索劫持工具栏拦截和更改等等 这样的事情总是应该在服务器端完成!

答案 1 :(得分:0)

最后我找到了答案,我在这里为其他用户提供, 我在html输入数组中有5个键,因此数组索引最多为4

if(!empty($_FILES['userfile']['tmp_name'][0]) or !empty($_FILES['userfile']['tmp_name'][1]) or !empty($_FILES['userfile']['tmp_name'][2]) or !empty($_FILES['userfile']['tmp_name'][3]) or !empty($_FILES['userfile']['tmp_name'][4])){
//at-least one file is selected so proceed to upload 
}else{
//no file selected, notify user
}

答案 2 :(得分:-1)

使用PHP(例如Check if specific input file is empty)有几种方法可以做到这一点,但是使用JS,它在服务器上更快,更便宜。使用jQuery,你可以这样做:

$.fn.checkFileInput = function() {
    return ($(this).val()) ? true : false;
}


if ($('input[type="file"]').checkFileInput()) {
    alert('yay');
}
else {
    alert('gtfo!');
}