【问题标题】:Need file type checking script compatible with IE [duplicate]需要与 IE 兼容的文件类型检查脚本 [重复]
【发布时间】:2012-12-08 10:42:05
【问题描述】:

可能重复:
uploaded file type check by PHP

我使用下面的脚本在上传时检查文件类型,以限制文件上传仅 jpg/jpeg、png。但它不适用于 IE(适用于 mozilla)。所以我需要一个适用于所有浏览器的脚本。

$destination_path = getcwd().DIRECTORY_SEPARATOR;

$target_path = $destination_path . basename( $_FILES['myfile']['name']);
$types=array('image/png','image/jpeg');

if (in_array($_FILES['myfile']['type'], $types))
{
if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {   
$result = 1;
}
}

else
{
$result = 0;
}

【问题讨论】:

    标签: php file-upload


    【解决方案1】:

    IE 为 jpg 设置了不同的文件类型标题,因此请将 image/pjpeg 添加到您的 $types

    $types=array('image/png','image/jpeg', 'image/pjpeg');
    

    【讨论】:

      【解决方案2】:

      在谷歌上花一点时间,你会发现它对所有人都非常有效 浏览器

      $allowedExts = array("jpg", "jpeg", "gif", "png");
      $extension = end(explode(".", $_FILES["file"]["name"]));
      $_FILES["file"]["name"]=$_FILES["file"]["name"];
      if ((($_FILES["file"]["type"] == "image/gif")
      || ($_FILES["file"]["type"] == "image/jpeg")
      || ($_FILES["file"]["type"] == "image/png")
      || ($_FILES["file"]["type"] == "image/pjpeg"))
      && ($_FILES["file"]["size"] < 50000)
      && in_array($extension, $allowedExts))
      {
      if ($_FILES["file"]["error"] > 0)
      {
      echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
      }
      else
      {
      if (file_exists("upload/" . $_FILES["file"]["name"]))
        {
        echo $_FILES["file"]["name"] . " already exists. ";
        }
      else
        {
        move_uploaded_file($_FILES["file"]["tmp_name"],
        "upload/" . $_FILES["file"]["name"]);
      
        $filename= "upload/" . $_FILES["file"]["name"];
        }
      }
       }
      else
      {
      echo "Invalid file";
      }
      

      【讨论】:

      • 不工作。我以前试过这个代码。
      • 有趣的是,我的网站上有这段代码,你检查过目录中没有相同的文件
      猜你喜欢
      • 1970-01-01
      • 2021-04-11
      • 2015-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-29
      • 1970-01-01
      相关资源
      最近更新 更多