基于用户的PHP显示消息有附加文件或不附加文件(第2部分) [英] PHP show message based on user had attach file or not attach file (part 2)

查看:139
本文介绍了基于用户的PHP显示消息有附加文件或不附加文件(第2部分)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



同样,表单:

 < html> 
< head>< / head>

< body>
< form action =add.phpmethod =postenctype =multipart / form-data>
档案1:< input type =filename =file1/>
档案2:< input type =filename =file2/>
< input type =submitname =submitvalue =ADD>
< / form>
< / body>
< / html>

用户只能附加jpg,jpeg,gif和png扩展名。这里是我的要求:
(1)如果用户上传jpg文件在文件1 --->是一个图像文件
(2)如果用户上传psd文件到文件1 --->不是图像文件
(3)如果用户在文件2中上传jpg文件--->是图像文件
(4)如果用户上传psd文件2 --->不是图像文件
(5)如果用户在文件1中上传jpg文件,文件2 --->中的psd文件不是图像文件< (6)如果用户上传文件1中的psd文件,文件2 --->中的jpg文件不是图像文件
(7)如果用户上传jpg文件文件1和文件2 --->是一个图像文件


以下是我试过的代码:

 <?php 
if(isset($ _ POST ['submit'])&&($ _POST ['submit'] =='ADD '))
{
if(empty($ _ FILES ['file1'] ['name'])&& empty($ _ FILES ['file2'] ['name']))
{
echo'2 files empty';
}
else
{
// HERE
$ allowed_extensions = array('jpg','jpeg','gif','png');
$ b $ foreach($ _ FILES as $ file)
{
$ name = $ file ['name'];

if(!empty($ name))
{
$ fileextension = strtolower(pathinfo($ name,PATHINFO_EXTENSION));

if(in_array($ fileextension,$ allowed_extensions))
{
echo'是一个图片文件';
}
else
{
echo'不是图像文件';




$ b code $


当我上传文件1上的jpg文件和文件2上的psd文件时,我得到以下输出:
是一个图像文件
不是一个图像文件

但是我的预期输出只显示不是图像文件。
我在做什么错?
我应该如何在这里继续它?有人可以帮我吗?

解决方案

试试这样

声明一个变量 $ flag = 0;

  if(in_array($ fileextension,$ allowed_extensions))
{
$ flag == 0? 0:1; //它的图像在这里,但检查标志是否已经是1,如果是1,那么不更新其他的作为0
}
else
{
$ flag = 1; //错误
}

after foreach echo'不是图像文件';
} else {
echo'是一个图片文件';

编辑

 <?php 
if(isset($ _ POST ['submit'])&&($ _POST ['submit'] == ($ _ FILES ['file1'] ['name'])&& empty($ _ FILES ['file2'] ['name']) )
{
echo'2 files empty';
}
else
{
// HERE
$ allowed_extensions = array('jpg','jpeg','gif','png');
$ b $ foreach($ _ FILES as $ file)
{
$ name = $ file ['name'];

if(!empty($ name))
{
$ fileextension = strtolower(pathinfo($ name,PATHINFO_EXTENSION));

if(in_array($ fileextension,$ allowed_extensions))
{
$ flag == 0? 0:1; //它的图像在这里,但检查标志是否已经是1,如果是1,那么不更新其他的作为0
}
else
{
$ flag = 1; //错误



$ b $ if($ flag){
echo'不是图像文件';
} else {
echo'是一个图片文件';
}
}
}


Cont. on

Again, the form:

<html>
<head></head>

<body>
<form action="add.php" method="post" enctype="multipart/form-data">
File 1: <input type="file" name="file1" />
File 2: <input type="file" name="file2" />
<input type="submit" name="submit" value="ADD">
</form>
</body>
</html>

User can only attach jpg, jpeg, gif and png extension. Here is my requirement:
(1) If user upload jpg file in file 1 ---> is an image file
(2) If user upload psd file in file 1 ---> is not an image file
(3) If user upload jpg file in file 2 ---> is an image file
(4) If user upload psd file in file 2 ---> is not an image file
(5) If user upload jpg file in file 1, psd file in file 2 ---> is not an image file
(6) If user upload psd file in file 1, jpg file in file 2---> is not an image file
(7) If user upload jpg file in file 1 and file 2 ---> is an image file

Here is the code that I tried:

<?php
if(isset($_POST['submit']) && ($_POST['submit'] == 'ADD'))
{
     if(empty($_FILES['file1']['name']) && empty($_FILES['file2']['name']))
     {
          echo '2 files empty';
     }
     else
     {
          //HERE
          $allowed_extensions = array('jpg','jpeg','gif','png'); 

          foreach($_FILES as $file)
          {
              $name = $file['name'];

              if(!empty($name))
              {
                  $fileextension = strtolower(pathinfo($name, PATHINFO_EXTENSION));

                  if(in_array($fileextension, $allowed_extensions))
                  {
                      echo 'is an image file';
                  }
                  else
                  { 
                      echo 'is not an image file';
                  } 
              }
          }
     }
}

When I upload a jpg file on file 1 and a psd file on file 2, I get the following output:
is an image file
is not an image file

But my expected output is only show "is not an image file". What am I doing wrong?
How should I continue it in HERE section? Someone can help me?

解决方案

Try like this

Outside loop declare a variable $flag = 0;

if(in_array($fileextension, $allowed_extensions))
{
   $flag == 0 ? 0 : 1; // its image here, but check if flag is already 1,if 1, dont update else make as 0
}
else
{ 
    $flag = 1; // error
} 

after foreach loop add this code.

if($flag) {
     echo 'is not an image file';
} else {
     echo 'is an image file';
}

EDIT

<?php
if(isset($_POST['submit']) && ($_POST['submit'] == 'ADD'))
{
     if(empty($_FILES['file1']['name']) && empty($_FILES['file2']['name']))
     {
          echo '2 files empty';
     }
     else
     {
          //HERE
          $allowed_extensions = array('jpg','jpeg','gif','png'); 

          foreach($_FILES as $file)
          {
              $name = $file['name'];

              if(!empty($name))
              {
                  $fileextension = strtolower(pathinfo($name, PATHINFO_EXTENSION));

                  if(in_array($fileextension, $allowed_extensions))
                  {
                        $flag == 0 ? 0 : 1; // its image here, but check if flag is already 1,if 1, dont update else make as 0
                  }
                  else
                  { 
                      $flag = 1; // error
                  } 
              }
          }

          if($flag) {
               echo 'is not an image file';
          } else {
               echo 'is an image file';
          }
     }
}

这篇关于基于用户的PHP显示消息有附加文件或不附加文件(第2部分)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆