正在上传JPG而不是jpg

时间:2016-02-16 21:01:09

标签: php upload

我有一个代码块,只要扩展名为小写jpg,就会上传图片并创建缩略图,但如果图像是大写的JPG,则不会上传图像。当我重命名图像时,例如example.JPG到example.jpg,它会上传。但是example.JPG不会上传,我没有错误。这对我没有任何意义。有没有人对此有解释?这是我的代码:

<?php
function createThumbnail($filename) {
  require 'config.php';

  if(preg_match('/[.](jpg)$/', $filename)) {
    $im = imagecreatefromjpeg($filename.$path_to_image_directory);
  } else if (preg_match('/[.](gif)$/', $filename)) {
    $im = imagecreatefromgif($path_to_image_directory . $filename);
  } else if (preg_match('/[.](png)$/', $filename)) {
    $im = imagecreatefrompng($path_to_image_directory . $filename);
  }

  $ox = imagesx($im);
  $oy = imagesy($im);

  $nx = $final_width_of_image;
  $ny = $final_height_of_image;

  $nm = imagecreatetruecolor($nx, $ny);

  imagecopyresampled($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);

  if(!file_exists($path_to_thumbs_directory)) {
    if(!mkdir($path_to_thumbs_directory)) {
       die("There was a problem. Please try again!");
    } 
  }

  imagejpeg($nm, $path_to_thumbs_directory . $filename);
  $tn = '<img src="' . $path_to_thumbs_directory . $filename . '" alt="image" />';
  $tn .= '<br />Congratulations. Your file has been successfully uploaded, and a      thumbnail has been created.';
  echo $tn;
}
?>

配置文件:

<?php  
$final_width_of_image = 300;
$final_height_of_image = 300;
$path_to_image_directory = '';
$path_to_thumbs_directory = '-tn';
?>     

2 个答案:

答案 0 :(得分:5)

在此代码块中,您只允许使用小写文件扩展名:

if(preg_match('/[.](jpg)$/', $filename)) {
//--lowercase-only--^ 
    $im = imagecreatefromjpeg($filename.$path_to_image_directory);
}else if (preg_match('/[.](gif)$/', $filename)) {
    $im = imagecreatefromgif($path_to_image_directory . $filename);
} else if (preg_match('/[.](png)$/', $filename)) {
    $im = imagecreatefrompng($path_to_image_directory . $filename);
}

您可以通过在正则表达式中放置OR condition来解决此问题:

if(preg_match('/[.](jpg|JPG)$/', $filename)) {
// -----uppercase-------^
    $im = imagecreatefromjpeg($filename.$path_to_image_directory);
}else if (preg_match('/[.](gif|GIF)$/', $filename)) {
    $im = imagecreatefromgif($path_to_image_directory . $filename);
} else if (preg_match('/[.](png|PNG)$/', $filename)) {
    $im = imagecreatefrompng($path_to_image_directory . $filename);
}

或者您可以使用i修饰符添加不区分大小写:

if(preg_match('/[.](jpg)$/i', $filename)) {
// -------modifier--------^ 
    $im = imagecreatefromjpeg($filename.$path_to_image_directory);
}else if (preg_match('/[.](gif)$/i', $filename)) {
    $im = imagecreatefromgif($path_to_image_directory . $filename);
} else if (preg_match('/[.](png)$/i', $filename)) {
    $im = imagecreatefrompng($path_to_image_directory . $filename);
}

答案 1 :(得分:0)

更好的方法是检查文件本身而不是扩展名,这可能总是错误的。

    $data = getimagesize($filename);
    switch($data['mime]){
        case('image/png):
            $im = imagecreatefrompng($filename);
            break;
        case('img/jpg'):
        case('image/jpeg'):
        case('image/pjpeg'):
        case('image/x-jps'):
            $im = imagecreatefromjpeg($filename);
            break;
        case(self::GIF):
            $im = imagecreatefromgif($filename);
            break;
    }