带有多个文件扩展名的PHP上传表单&大写

时间:2011-01-11 22:53:47

标签: php forms upload

我的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";
}
?>

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

4 个答案:

答案 0 :(得分:2)

除了@Swanny,你还有几个选择:

in_array方法(加上strtolower):

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

使用strcasecmp来消除案例:

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

或者,good'ol'成为RegEx apprach(取消了$ext变量:

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

也可以像这样进行修改(添加以竖线(|)字符分隔的其他扩展名)

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

答案 1 :(得分:1)

你可能会改变第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

答案 2 :(得分:1)

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

$ext = strtolower($ext);

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

答案 3 :(得分:0)

试试这个。使用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"