Annoying php code lol

  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 477

Post 3+ Months Ago

OK so this is not an issue that I caused lol. This is code someone else wrote and the people always had issues with. The code has to do with images. I have known about this issue for years and have always done this correctly, however the person who wrote this did not know about this issue. So I'm posting this for others to know if they didn't know already.

Simple example (bad example)
PHP Code: [ Select ]
$path_parts = pathinfo($_FILES['photo']['name']);
$target = "y2013/int" . $id . '.' . $path_parts['extension'];
 
// Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) {
    echo "Yay! I'm on fire!";
}
 
  1. $path_parts = pathinfo($_FILES['photo']['name']);
  2. $target = "y2013/int" . $id . '.' . $path_parts['extension'];
  3.  
  4. // Writes the photo to the server
  5. if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) {
  6.     echo "Yay! I'm on fire!";
  7. }
  8.  

HTML Code: [ Select ]
<img id="photo<?php echo $info['id']; ?>" src="y2013/int<?php echo $info['id']; ?>.jpg" />
 
  1. <img id="photo<?php echo $info['id']; ?>" src="y2013/int<?php echo $info['id']; ?>.jpg" />
  2.  


The reason this is bad, people for some reason like THE CAPS LOCK BUTTON so the filename can be HEY_LOOK_IM_A_FILE.JPG. so the image above will break because its using a lowercase extension instead of an uppercase one.

Simple fix set the extension to uppercase or lowercase when saving the file.

This THING I'm fixing is for some kind of photo contest which at the current moment has logged over 10,000 entries and more are coming in. Now they at least are coming in low cased extensions. Again I did not write this, this monstrosity of a program. I would have gone another route.

the quick fix to stop caps extensions that I've implemented for the time being at least
PHP Code: [ Select ]
$path_parts = pathinfo($_FILES['photo']['name']);
$target = "y2013/int" . $id . '.' . strtolower($path_parts['extension']); // <---- Here Yay! STOP WITH THE CAPS!!
 
// Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) {
    echo "Awww! I wanted to explode.";
}
 
  1. $path_parts = pathinfo($_FILES['photo']['name']);
  2. $target = "y2013/int" . $id . '.' . strtolower($path_parts['extension']); // <---- Here Yay! STOP WITH THE CAPS!!
  3.  
  4. // Writes the photo to the server
  5. if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) {
  6.     echo "Awww! I wanted to explode.";
  7. }
  8.  


If you're wondering about all on the other files sure I could make a program to run through and rename them or download them get a renaming program to deal with it and re-upload
but the path i chose was this

PHP Code: [ Select ]
 
$exts = array("jpg", "JPG", "JPEG", "jpeg", "png", "PNG");
 
foreach($exts as $key => $value) {
    if(file_exists('y2013/int'.$info['id'] .'.'. $value)) {
        $con_ext = $value;
    }
}
 
  1.  
  2. $exts = array("jpg", "JPG", "JPEG", "jpeg", "png", "PNG");
  3.  
  4. foreach($exts as $key => $value) {
  5.     if(file_exists('y2013/int'.$info['id'] .'.'. $value)) {
  6.         $con_ext = $value;
  7.     }
  8. }
  9.  
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

Post Information

  • Total Posts in this topic: 1 post
  • Users browsing this forum: No registered users and 51 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.