【问题标题】:PHP upload form with multiple file extensions & uppercase具有多个文件扩展名和大写的 PHP 上传表单
【发布时间】:2011-06-07 12:13:59
【问题描述】:

我的 php 上传表单正常工作(允许 .jpg),但不知道如何添加 .JPG(大写问题)或 .jpeg。

有人可以告诉我如何将这些扩展添加到以下代码中吗?

<?php
//–°heck that we have a file
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
  //Check if the file is JPEG image and it's size is less than 350Kb
  $filename = basename($_FILES['uploaded_file']['name']);
  $ext = substr($filename, strrpos($filename, '.') + 1);
  if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && 
    ($_FILES["uploaded_file"]["size"] < 16000000)) {
    //Determine the path to which we want to save this file
      $newname = dirname(__FILE__).'/upload/'.$filename;
      //Check if the file with the same name is already exists on the server
      if (!file_exists($newname)) {
        //Attempt to move the uploaded file to it's new place
        if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
           echo "It's done! The file has been saved!";
        } else {
           echo "Error: A problem occurred during file upload!";
        }
      } else {
         echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
      }
  } else {
     echo "Error: Only .jpg images under 15 Mb are accepted for upload";
  }
} else {
 echo "Error: No file uploaded";
}
?>

非常感谢您的帮助和时间!

【问题讨论】:

    标签: php forms upload


    【解决方案1】:

    除了@Swanny,您还有更多选择:

    in_array 方法(加上strtolower):

    if (in_array(strtolower($est),Array('jpg','jpeg')))
    

    有使用strcasecmp 消除大小写:

    if (strcasecmp($ext,'jpg') === 0 || strcasecmp($ext,'jpeg') === 0)
    

    或者,好的'ol'fashioned RegEx apprach(消除了$ext变量:

    if (preg_match('/\.(jpe?g)$/i',$filename))
    

    也可以这样修改(添加由竖线(|)字符分隔的附加扩展名)

    if (preg_match('/\.(jpe?g|gif|png)$/i',$filename))
    

    【讨论】:

      【解决方案2】:

      您可能会将第 6 行更改为:

      if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") &&
      

      到:

      if (($ext == "jpg" || $ext == "jpeg" || $ext == "JPG") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") &&
      

      虽然如果您想进一步扩展它,我建议您使用文件类型数组并检查数组,这样更容易管理。具体使用in_array函数http://php.net/manual/en/function.in-array.php

      【讨论】:

      • 非常感谢您的帮助!你救了我这个。
      【解决方案3】:

      比较时将扩展名转换为小写。

      $ext = strtolower($ext);
      
      if ( ($ext == 'jpg' || $ext == 'jpeg') && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && 
          ($_FILES["uploaded_file"]["size"] < 16000000)) {
      

      【讨论】:

        【解决方案4】:

        试试这个。使用 strtolower() 将当前文件的扩展名转换为小写,然后使用 in_array() 检查接受的扩展名数组:

        <?php
        $acceptedExts = array ('jpg','jpeg');
        
        //–check that we have a file
        
        if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
          //Check if the file is JPEG image and it's size is less than 350Kb
          $filename = basename($_FILES['uploaded_file']['name']);
          $ext = strtolower (substr($filename, strrpos($filename, '.') + 1));
          if (in_array($ext,$acceptedExts) && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && 
            ($_FILES["uploaded_file"]["size"] < 16000000)) {
            //Determine the path to which we want to save this file
              $newname = dirname(__FILE__).'/upload/'.$filename;
              //Check if the file with the same name is already exists on the server
              if (!file_exists($newname)) {
                //Attempt to move the uploaded file to it's new place
                if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
                   echo "It's done! The file has been saved!";
                } else {
                   echo "Error: A problem occurred during file upload!";
                }
              } else {
                 echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
              }
          } else {
             echo "Error: Only .jpg images under 15 Mb are accepted for upload";
          }
        } else {
         echo "Error: No file uploaded";
        }
        ?>
        

        请注意,它不适用于其他图像类型,因为您仍在检查文件类型是否为 image/jpeg:

        $_FILES["uploaded_file"]["type"] == "image/jpeg"
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-04-02
          • 1970-01-01
          • 1970-01-01
          • 2016-07-26
          • 1970-01-01
          • 1970-01-01
          • 2020-01-11
          • 1970-01-01
          相关资源
          最近更新 更多