Jump to content

Image Upload Validation


mattyvx

Recommended Posts

Hi, i've have a php script which lets users upload profile pictures to my site. The script renames the image to the users ID (where $ID is set), saves it in a directory then mails me to let me know the pic has changed.

 

HOWEVER, some times it does not work. I think its functionality various depending on if the user is using firefox or IE?!?

 

My goal is to limit the user to only uploading .gif or jpg files.

 

Can you take a look at the following code and critique please;

 

function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
$ext = findexts ($_FILES['file']['name']) ; 
$file = $ID.".";
$name = $file.$ext;
if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 10000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    if (file_exists("profiles/images/$filegif"))
      {
unlink("profiles/images/$filegif");
      }  
    if (file_exists("profiles/images/$filejpeg"))
      {
unlink("profiles/images/$filejpeg");
      }
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "profiles/images/" .$name);
     }

$sendto = "[email protected]";
$subject = "Profile Picture Update - $ID"; 
$message = "Customer ID : $ID 
     
$ID has updated their profile picture:

http://www.mysite/profiles/$ID
______________________________________________
     
This is an automated response, please do not reply!"; 
     
mail($sendto, $subject, $message, "From: Profile Picture Update <me@me>" . phpversion());   

echo "<meta http-equiv=\"refresh\" content=\"5;url=/profiles/$ID\" />";
echo '<h1 class="calign">Profile picture Updating - Please Wait</h1>';
echo '<br/><br/><br/><br/>';
echo '<img src="/images/loading.gif"  />';
  }
  else
  {
  echo "Invalid file - Only Gif or Jpeg files may be uploaded.";
  
  $e_subject = "Unable to upload profile pic | $ID"; 
  $e_message = "
    
There has been an error or invalid file upload attempt by $ID

The error code is: $error
______________________________________________
     
This is an automated response, please do not reply!"; 
     
mail("[email protected]", $e_subject, $e_message, "From: Error Reporting <[email protected]>" . phpversion());   
  }
}
else
{
?>

Link to comment
https://forums.phpfreaks.com/topic/201306-image-upload-validation/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.