PHP 上传 IF ISSET 总是说它是?

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

PHP Upload IF ISSET always says it is?

phpimageupload

提问by uollaa

OK, i have some kind of strange problem with my code. I'm trying to make uploading form with text along photos. Now, I need to check if the photo is uploaded or the text is inserted only.

好的,我的代码有一些奇怪的问题。我正在尝试制作带有照片文字的上传表单。现在,我需要检查照片是否已上传或仅插入文本。

The simplified code looks like this:

简化后的代码如下所示:

if (isset ($_FILES['image'])){
echo 'yes';


} else {    
echo 'no';      
}

the form looks like:

表格看起来像:

   <form action=""  method="post" enctype="multipart/form-data" class="contact-form">
   <form action=""  method="post" enctype="multipart/form-data" class="contact-form">


   <label for="title"><strong>Title</strong> (required)</label>
   <input type="text" name="title" value="" required>


   <label><span class="small"><strong>Add a photo</strong></span></label>
   <input type="file" name="image"/>


   <input type="submit" name"submit" value="Submit">    
   </form>

Now, basically, what I'm trying to make is a check if file is attached or not, because I need an alternate option for posting posts without photos, in the same form; but, the problem is that no mather if it is attached or not it always says YES.

现在,基本上,我要做的是检查文件是否已附加,因为我需要一个替代选项来以相同的形式发布没有照片的帖子;但是,问题是,无论是否附加,它总是说是。

Help, anyone.

帮助,任何人。

回答by Jimbo

To check if the file has been uploaded, how about:

要检查文件是否已上传,请执行以下操作:

if (!file_exists($_FILES['image']['tmp_name']) || !is_uploaded_file($_FILES['image']['tmp_name'])) 
{
    echo 'No upload';
}
else
{
    // Your file has been uploaded
}

is_uploaded_file()is the choice here, check the docs out for it.

is_uploaded_file()是这里的选择,请查看文档。

$_FILESis an array of files, and each file in the first dimension of the array is automatically given a ['tmp_name']key in the second dimension, with the value being it's temporary location.

$_FILES是一个文件数组,数组第一维中的每个文件都会自动['tmp_name']在第二维中给出一个键,值是它的临时位置。

Obviously you then need to use move_uploaded_file()on the temporary file.

显然,您需要move_uploaded_file()在临时文件上使用。

回答by bystwn22

You are checking only that $_FILES['image']passed from your form.
But you are not checking that posted $_FILES['image']is empty or its contains a valid file data
Read more here http://www.php.net/manual/en/features.file-upload.php

您正在检查仅从$_FILES['image']您的表单传递的内容。
但是您没有检查发布的内容$_FILES['image']是否为空或其包含有效的文件数据
在此处阅读更多信息http://www.php.net/manual/en/features.file-upload.php

<?php
  if ( isset( $_FILES["image"] ) && !empty( $_FILES["image"]["name"] ) ) {
    if ( is_uploaded_file( $_FILES["image"]["tmp_name"] ) && $_FILES["image"]["error"] === 0 ) {
      // everything okay, do process
      echo 'yes';
      exit();
    }
  }
  echo 'no';
?>

回答by Stacy Davidson

The reason "isset" doesn't work is most likely because $_FILES is considered set even when it is set to "null". I found the empty() suggestion to be the cleanest. Although, unless you're using a large form with multiple files, I would remove the brackets entirely.

“isset”不起作用的原因很可能是因为即使将 $_FILES 设置为“null”,也认为它已设置。我发现 empty() 建议是最干净的。虽然,除非您使用包含多个文件的大型表单,否则我会完全删除括号。

if (empty ($_FILES)){
 echo 'No file';
 } else {    
 echo 'File found';
 }

回答by Drew Lemmy

Use empty() instead.

使用 empty() 代替。

if (!empty ($_FILES['image'])){
    echo 'yes';


} else {    
    echo 'no';      
}