Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 954317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:11:48+00:00 2026-05-16T00:11:48+00:00

I’ve been building an event promotion site in PHP and MySQL for the past

  • 0

I’ve been building an event promotion site in PHP and MySQL for the past couple of months where anyone can sign up and add their local event’s details along with a poster, which I resize.

As it stands, I’ve got the whole process working fine locally and on a hosting service, but before the site goes live I have a couple of questions on the way I’m doing it.

This is the function code I’m using to handle the image uploads. I check for filesize before this section.

$extension = substr($filename, strpos($filename,'.'), strlen($filename)-1); 
$filetypes = array('.jpg', '.jpeg', '.gif', '.bmp', '.png', '.JPG', '.PNG', '.JPEG', '.GIF', '.BMP');
if($_FILES['image']['error'] == 4){
  $error = "No image";
  return $error; 
}
else if(($_FILES['image']['error'] == 2) || ($_FILES['image']['error'] == 1)){
  $error = "File size too big";
  return $error;
}
else if(!in_array($extension, $filetypes)){
  $error = "This isn't an image that is supported";
  return $error;
}
else if(($_FILES['image']['error'] == 7) || ($_FILES['image']['error'] == 3)){
  $error = "Error occurred. Try again";
  return $error;
}
else{
  if(($extension == '.jpg') || ($extension == '.jpeg')){
    $source = imagecreatefromjpeg($uploaded);
  }
  else if($extension == '.png'){
    $source = imagecreatefrompng($uploaded);
  }
  else{
    $source = imagecreatefromgif($uploaded);
  }
  list($width, $height) = getimagesize($uploaded);
  $ratio = $width / $height;
  $new_width = 300;
  $new_height = round(300 / $ratio); 
  $canvas = imagecreatetruecolor($new_width, $new_height);
  imagecopyresampled($canvas, $source, 0, 0, 0, 0, $new_width, $new_height, $width,       $height);
  $name = date("dmyHis").rand(0, 9);
  $path = $_SERVER[ 'DOCUMENT_ROOT' ] . '/images/uploaded/'.$name.'.jpg';
  $new_image = imagejpeg($canvas, $path,  100);
  $poster['name'] = $name.'.jpg';
  $poster['width'] = $new_width;
  $poster['height'] = $new_height;
  return $name.'.jpg';
}

As it stands, there are a couple of bugs that I know about, or haven’t fully looked into, such as some images throwing an error from imagecreatefromwhatever, and if the image name has a ‘.’ in it, it’ll also throw an error.

Once the process is done, I’ll save the image name into a ‘poster’ field in MySQL, which will be used to get the correct image from the folder when being viewed.

What I really wanted to know is if there’s any other problems I’m likely to face with image uploads?

  • I’m expecting a fair amount of traffic, so is this code going to run alright with heavy usage?
  • Are there any other pitfalls or things I should be looking out for?
  • Am I using the best method for the job?
  • My filesize limit at the moment is 2MB, is this too high?
  • Even if a user uploads something over 2MB, the script will still run, and I assume the file will be uploaded to the server for name stripping and filesize comparison etc., how will this affect my bandwidth usage?
  • How long do original files stay on the server?

If anyone has any good reading on the subject I would much appreciate it!

Thank you.

edit: Formatting.

edit 2: I didn’t make myself clear about the original files. What I mean is the original files that I use the $_FILES variable to access. Say it’s 1.9MB, will there be 1.9MB’s worth of image sitting on the server the whole time I’m fiddling with the extensions and that? Should I clear this once I’ve created a new image?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:11:49+00:00Added an answer on May 16, 2026 at 12:11 am

    The extension as sent by the user in the filename can’t be trusted or relied upon. Some users think changing ‘jpg’ to ‘gif’ makes it a gif, etc.

    I suggest using getimagesize FIRST to check if it’s a valid image and to get the exif type. DOn’t worry about extracting the extension, since it’s useless. The exif type will be in 2 of the array returned by getimagesize.

    Also, CYMK images are a problem. Some people manage to upload CYMK jpegs. Checking channels will detect these images. It should be 3, RGB.

    $image_info=getimagesize($your_image_file);
    if($image_info['channels']==4)
      {
      //it's invalid - cymk
      //browsers cannot display these images. It might be possible to convert them to RGB explicitly...
      }
    
    $real_exif=$image_info[2];
    if($real_exif>0 && $real_exif<4){
     //it is a png, gif or jpg
     }
    

    The exif type is returned as a constant like IMAGETYPE_GIF, where numerically, 1 is gif, 2 is jpg, and 3 is png. You can use image_type_to_extension to convert to a text file extension.

    Now, sometimes I found getimagesize failed to find exif type for images that were valid, and could be worked on with imagemagick/GD. It was failing to return an EXIF for these, so they were incorrectly rejected. I came up with this vile hack to at least detect the type and give them a try…

      $handle=@fopen($temp_name,'r');
      if($handle)
        {
        $chars=fread($handle,24);
        if(stripos($chars,'jfif')!==false)
            {$type=2;} // found a jpg
        elseif(stripos($chars,'png')!==false)
          {$type=3;} // found a png
        elseif(stripos($chars,'gif')!==false)
          {$type=1;} // found a gif
        else
          {
          //file type could not be determined
          }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.