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 8064165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:17:13+00:00 2026-06-05T11:17:13+00:00

I want to put out an error message when the uploaded image size is

  • 0

I want to put out an error message when the uploaded image size is over 3MB. This is my current code. It should put out an error message when an image is over 3MB, but it does nothing. What’s wrong with my code?

if ( $_FILES['file']['size'] != 0 ) 
{
 //image check start
 if ((($_FILES["file"]["type"] == "image/gif")
 || ($_FILES["file"]["type"] == "image/jpeg")
 || ($_FILES["file"]["type"] == "image/png")
 || ($_FILES["file"]["type"] == "image/pjpeg"))
 && ($_FILES["file"]["size"] < 3072000))
 //image check end
 {
      if (file_exists($upload_path."/".$_FILES["file"]["name"])) 
      {
           $file_tmp = $_FILES["file"]["name"];
      } //Link if there is already a file with identical file name
      else
      {
           $photoid = $upfile_idx.".".substr($_FILES['file']['name'],-3);
           move_uploaded_file($_FILES["file"]["tmp_name"], $upload_path."/".$photoid);
           $file_tmp = $photoid ;
      } //Upload the image file into upload folder and generate an id for the image
  }
  else
  {
      error("Maximum image size exceeded or invalid file format.");
  }
}

//insert $file_tmp into database here

----------
Error code (added later)
function error($msg)
{
  echo "<script>alert(\"$msg\");history.go(-1);</script>";
  exit;
}

I’ve found what’s wrong. In my php.ini file, there was ‘upload_max_filesize = 3M’ and obviously that’s what was causing all the problem. When I changed it to ‘upload_max_filesize = 4M’, everything worked just fine. Thank you all for your help.

  • 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-06-05T11:17:14+00:00Added an answer on June 5, 2026 at 11:17 am

    Something on the following should do you good.

    <?php 
    if ( $_FILES['file']['size'] != 0 ) 
    {
     //image check start
    
     if(!in_array($_FILES["file"]["type"], array("image/gif", "image/jpeg", "image/png", "image/pjpeg")))
     {
        error("File should be a JPG/JPEG/PNG/PJEPG only");
        return; 
     }
     if($_FILES["file"]["size"] > 3145728)
     {
        error("File should be less than 3 MB");   
        return; 
     }
     #Rest of your upload code should go below here.
    }
    ?> 

    3145728 translates to 3 MB in bytes since $_FILES[“file”][“size”] gives the size of the file being uploaded in bytes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to put out an error message when the uploaded image size is
A Phusion Passenger error message isn't what I want my visitors to see if
I've got a simple query where I want to put the current date var
I've projected an Intranet Ajax application and I want put it in a full
hey, hi i want put limit on object creation means a class can have
I want to put a measure in place to stop people from trying to
I want to put the result of the sort method into an array where
I want to put an info light button in a section header - (CGFloat)tableView:(UITableView
I want to put 2 dock windows in LeftDock area in Qt3. Since LeftDock
I want to put in may application a button which will give a possibility

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.