php 检查特定输入文件是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14458553/
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 07:18:28  来源:igfitidea点击:

Check if specific input file is empty

php

提问by Sasha

In my form I have 3 input fields for file upload:

在我的表单中,我有 3 个用于文件上传的输入字段:

<input type=file name="cover_image">
<input type=file name="image1">
<input type=file name="image2">

How can I check if cover_imageis empty - no file is put for upload?

如何检查是否cover_image为空 - 没有文件可供上传?

回答by Rudi Visser

You can check by using the sizefield on the $_FILESarray like so:

您可以使用数组size上的字段进行检查,$_FILES如下所示:

if ($_FILES['cover_image']['size'] == 0 && $_FILES['cover_image']['error'] == 0)
{
    // cover_image is empty (and not an error)
}

(I also check errorhere because it may be 0if something went wrong. I wouldn't use namefor this check since that can be overridden)

(我也在error这里检查,因为这可能是因为出现0问题。我不会name用于此检查,因为它可以被覆盖)

回答by Techie

Method 1

方法一

if($_FILES['cover_image']['name'] == "") {
// No file was selected for upload, your (re)action goes here
}

Method 2

方法二

if($_FILES['cover_image']['size'] == 0) {
// No file was selected for upload, your (re)action goes here
}

回答by BenM

You can check if there is a value, and if the image is valid by doing the following:

您可以通过执行以下操作来检查是否有值,以及图像是否有效:

if(empty($_FILES['cover_image']['tmp_name']) || !is_uploaded_file($_FILES['cover_image']['tmp_name']))
{
   // Handle no image here...
}

回答by Rakesh Sharma

if (empty($_FILES['cover_image']['name']))

回答by Bruno Ribeiro

simple :

简单的 :

if($_FILES['cover_image']['error'] > 0)
    // cover_image is empty

回答by Johny

 if( ($_POST) && (!empty($_POST['cover_image'])) )    //verifies  if post exists and cover_image is not empty
    {
    //execute whatever code you want
    }

回答by user7282

check after the form is posted the following

在表格发布后检查以下内容

$_FILES["cover_image"]["size"]==0

回答by Prabhagaran

if($_FILES['img_name']['name']!=""){
   echo "File Present";
}else{
  echo "Empty file";
}

回答by Parth Dhorda

if ($_FILES['cover_image']['size'] == 0 && $_FILES['cover_image']['error'] == 0)
{ 
      // Code comes here
}

This thing works for me........

这东西对我有用......