Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading JPG but not jpg

Tags:

php

upload

I have a block of code that uploads an image and creates a thumbnail as long as the extension is lowercase jpg, but it will not upload an image if it is uppercase JPG. When I rename the image, e.g. example.JPG to example.jpg, it will upload. But example.JPG will not upload and I get no error. This does not make any sense to me. Does anyone have an explanation to this? Here is my code:

<?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;
}
?>

The config file:

<?php  
$final_width_of_image = 300;
$final_height_of_image = 300;
$path_to_image_directory = '';
$path_to_thumbs_directory = '-tn';
?>     
like image 403
Derreck Avatar asked Sep 26 '22 08:09

Derreck


1 Answers

In this code block you're only allowing lowercase file extensions:

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);
}

You can fix by placing an OR condition in the regex:

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);
}

Or you can add case insensitivity with the i modifier:

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);
}
like image 117
Jay Blanchard Avatar answered Oct 18 '22 04:10

Jay Blanchard