如何在PHP中允许某些扩展类型?

时间:2015-11-20 03:00:29

标签: php file-upload mime-types file-extension

我有3个文件输入,我一直试图保证,但我还没有成功。 我需要这些文件输入才能使用 jpeg,jpg,png, gif 。我知道 mime类型使用起来不可靠,因此,我想不再使用它。如果以 PHP程序方式而不是 IF语句进行更清洁或更快捷的方式,那就更好了。

HTML code

<input type="file" name="index_desl_Cfile1" class="upload-image" />
<input type="file" name="index_desl_Cfile2" class="upload-image" />
<input type="file" name="index_desl_Cfile3" class="upload-image" />

PHP代码

$target_dir = "../site_images/";

$index_deslC1 = $target_dir . basename($_FILES["index_desl_Cfile1"]["name"]);
$index_deslC2 = $target_dir . basename($_FILES["index_desl_Cfile2"]["name"]);
$index_deslC3 = $target_dir . basename($_FILES["index_desl_Cfile3"]["name"]);

// Check if file already exists
 $src1 = 'http://localhost//397/admin/site_images/'.$index_deslC1;
 $src2 = 'http://localhost/397/admin/site_images/'.$index_deslC2;
 $src3 = 'http://localhost/397/admin/site_images/'.$index_deslC3;    
if (@getimagesize($src1)) {
    echo "Sorry, file already exists 1. ";
    $uploadOk = 0;

}
 else if (@getimagesize($src2)) {
    echo "Sorry, file already exists 2. ";
    $uploadOk = 0;
}
 else if (@getimagesize($src3)) {
    echo "Sorry, file already exists 3. ";
    $uploadOk = 0;
}

    $imageFileTypeC1 = $_FILES["index_desl_Cfile1"]["type"];
    $imageFileTypeC2 = $_FILES["index_desl_Cfile2"]["type"];
    $imageFileTypeC3 = $_FILES["index_desl_Cfile3"]["type"];

    $allowed_types = array('image/jpg','image/png','image/jpeg','image/gif');

    if (!in_array($imageFileTypeC1, $allowed_types)) {

            echo "ILLEGAL FILE TYPE 1";
            $uploadOk = 0;

        }
    else if (!in_array($imageFileTypeC2, $allowed_types)) {

            echo "ILLEGAL FILE TYPE 2";
            $uploadOk = 0;

        }
    else if (!in_array($imageFileTypeC3, $allowed_types)) {

            echo "ILLEGAL FILE TYPE 3";
            $uploadOk = 0;

        }           
         else {
            $uploadOk = 1;
        }
    if ($uploadOk == 0) {
        echo " Sorry, your file was not uploaded.";
    // if everything is ok, try to upload file
    } else {
        if (move_uploaded_file($_FILES["index_desl_Cfile1"]["tmp_name"], $index_deslC1)) {
            echo "The file ". basename( $_FILES["index_desl_Cfile1"]["name"]). " has been uploaded.";
        }
  else if (move_uploaded_file($_FILES["index_desl_Cfile2"]["tmp_name"], $index_deslC2)) {
            echo "The file ". basename( $_FILES["index_desl_Cfile2"]["name"]). " has been uploaded.";
        }
  else if (move_uploaded_file($_FILES["index_desl_Cfile3"]["tmp_name"], $index_deslC3)) {
            echo "The file ". basename( $_FILES["index_desl_Cfile3"]["name"]). " has been uploaded.";
        }

它将有超过3个文件输入。

1 个答案:

答案 0 :(得分:0)

我终于明白了。我没有使用 if语句检查文件类型,而是使用了开关语句,它就像一个魅力!

以下是所有代码......

$target_dir = "../site_images/";

$index_deslC1 = $target_dir . basename($_FILES["index_desl_Cfile1"]["name"]);
$index_deslC2 = $target_dir . basename($_FILES["index_desl_Cfile2"]["name"]);
$index_deslC3 = $target_dir . basename($_FILES["index_desl_Cfile3"]["name"]);

// Check if file already exists
$src1 = 'http://localhost//397/admin/site_images/'.$index_deslC1;
$src2 = 'http://localhost/397/admin/site_images/'.$index_deslC2;
$src3 = 'http://localhost/397/admin/site_images/'.$index_deslC3;    
if (@getimagesize($src1)) {
echo "Sorry, file already exists 1. ";
$uploadOk = 0;

}
else if (@getimagesize($src2)) {
echo "Sorry, file already exists 2. ";
$uploadOk = 0;
}
else if (@getimagesize($src3)) {
echo "Sorry, file already exists 3. ";
$uploadOk = 0;
}

$imageFileTypeC1 = $_FILES["index_desl_Cfile1"]["type"];
$imageFileTypeC2 = $_FILES["index_desl_Cfile2"]["type"];
$imageFileTypeC3 = $_FILES["index_desl_Cfile3"]["type"];

$allowed_types = array('image/jpg','image/png','image/jpeg','image/gif');

switch ($allowed_types) {
    case in_array($imageFileTypeC1, $allowed_types):
      echo "GOOD 1!";
      $uploadOk = 1;
    break;
    case in_array($imageFileTypeC2, $allowed_types):
      echo "GOOD 2!";
      $uploadOk = 1;
    break;
    default:
      echo "ILLEGAL FILE TYPE";
      $uploadOk = 0;
}

 if ($uploadOk == 0) {
    echo " Sorry, your file was not uploaded.";
 // if everything is ok, try to upload file
 } else {
    if (move_uploaded_file($_FILES["index_desl_Cfile1"]["tmp_name"], $index_deslC1)) {
        echo "The file ". basename( $_FILES["index_desl_Cfile1"]["name"]). " has been uploaded.";
    }
else if (move_uploaded_file($_FILES["index_desl_Cfile2"]["tmp_name"], $index_deslC2)) {
        echo "The file ". basename( $_FILES["index_desl_Cfile2"]["name"]). " has been uploaded.";
    }
else if (move_uploaded_file($_FILES["index_desl_Cfile3"]["tmp_name"], $index_deslC3)) {
        echo "The file ". basename( $_FILES["index_desl_Cfile3"]["name"]). " has been uploaded.";
    }