PHP show message based on user had attach file or

2019-08-12 04:16发布

问题:

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?

回答1:

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';
          }
     }
}