php 如何检查codeigniter中的文件字段是否为空?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13953421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:20:29  来源:igfitidea点击:

How to check if file field is empty in codeigniter?

phpcodeigniterfile-upload

提问by squiter

I have a form with a file field called image, but this field is not required.
When user don't choose any file in form, the do_upload()always return a error.
How can I check if user chosen a file before perform the upload action in my controller?

我有一个名为 的文件字段的表单image,但该字段不是必需的。
当用户没有在表单中选择任何文件时,do_upload()总是返回一个错误。
在我的控制器中执行上传操作之前,如何检查用户是否选择了文件?

回答by Vinoth Babu

Please use empty()

请使用空()

if (empty($_FILES['userfile']['name'])) {
}

回答by Matanya

In your controller, on the function that receives the submitted form:

在您的控制器中,在接收提交表单的函数上:

if (isset($_FILES['image']['name']) && !empty($_FILES['image']['name'])) {
    // do_upload

}

回答by BenM

Try to check if the file is valid using is_uploaded_file(). For example:

尝试使用is_uploaded_file(). 例如:

if(is_uploaded_file($_FILES['userfile']['tmp_name'])) 
{  
   do_upload();
}

回答by shiv

Here is full script to check if file field is empty or not in php

这是检查 php 中文件字段是否为空的完整脚本

<!DOCTYPE html>
<html>
<body>

<form action="#" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input name="my_files[]" type="file" multiple="multiple" />
    <input type="submit" value="Upload Image" name="submit">
</form>


<?php

 if (isset($_FILES['my_files']))
  {
    $myFile = $_FILES['my_files'];
    $fileCount = count($myFile["name"]);


        for ($i = 0; $i <$fileCount; $i++)
         {
           $error = $myFile["error"][$i]; 

            if ($error == '4')  // error 4 is for "no file selected"
             {
               echo "no file selected";
             }
            else
             {

               $name =  $myFile["name"][$i];
               echo $name; 
               echo "<br>"; 
               $temporary_file = $myFile["tmp_name"][$i];
               echo $temporary_file;
               echo "<br>";
               $type = $myFile["type"][$i];
               echo $type;
               echo "<br>";
               $size = $myFile["size"][$i];
               echo $size;
               echo "<br>";



               $target_path = "uploads/$name";   //first make a folder named "uploads" where you will upload files


                 if(move_uploaded_file($temporary_file,$target_path))
                  {
                   echo " uploaded";
                   echo "<br>";
                   echo "<br>";
                  }
                   else
                  {
                   echo "no upload ";
                  }




              }
        }  
}
        ?>


</body>
</html>

But be alert. User can upload any type of file and also can hack your server or system by uploading a malicious or php file. In this script there should be some validations.

但要警惕。用户可以上传任何类型的文件,也可以通过上传恶意或 php 文件来入侵您的服务器或系统。在这个脚本中应该有一些验证。

refer http://www.techzigzag.com/how-to-check-that-user-has-upload-any-file-or-not-in-php/

参考http://www.techzigzag.com/how-to-check-that-user-has-upload-any-file-or-not-in-php/

Hope it will help you.

希望它会帮助你。

回答by jones

Just use native php code to check file upload.

只需使用本机 php 代码来检查文件上传。

if(!file_exists($_FILES['myfile']['tmp_name']) || !is_uploaded_file($_FILES['myfile']['tmp_name'])) {
echo 'No upload';
 }

回答by prasanthnv

if(!empty($_FILES[$file_name]['name'])){
  // TODO your logic

}else{
  echo "empty";
}

回答by Rauli Rajande

As file upload error "No file selected" is number 4, correct way of doing this is:

由于文件上传错误“未选择文件”是数字 4,正确的做法是:

if ($_FILES['my_image_field_name']['error'] !== 4){
    if ($this->upload->do_upload('my_image_field_name')) { ...

When checking by name or tmp_name, there might be other reasons why these fields didn't get populated, and you may miss these.

在按名称或 tmp_name 检查时,可能有其他原因导致这些字段未填充,您可能会错过这些。

回答by Ghostman

use empty()empty function does check if the file field is empty or not

使用empty()空函数会检查文件字段是否为空

if ( ! empty($_FILES)) {...}


if(!empty($_FILES['myFileField'])) {
    // file field is not empty..
} else {
    // no file uploaded..
}