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.

verisimilitude
  • 5,077
  • 3
  • 30
  • 35
gyogyo0101
  • 61
  • 1
  • 2
  • 10
  • Horrible code indentation/formatting or the lack of it thereof.. – verisimilitude Jun 12 '12 at 08:17
  • @verisimilitude know. That's because I'm a newbie. Isn't this site's purpose to teach and learn about programming? – gyogyo0101 Jun 12 '12 at 08:19
  • Please post the content of the error function as well. – sofl Jun 12 '12 at 08:20
  • 1
    @gyogyo0101: Learning to write "properly indented code" (which is readable) should be amongst the very first of your objectives while learning programming! – verisimilitude Jun 12 '12 at 08:23
  • @sofl added the function – gyogyo0101 Jun 12 '12 at 08:29
  • 1
    @verisimilitude I see. I'll look up for it. – gyogyo0101 Jun 12 '12 at 08:29
  • _"but it does nothing"_ That doesn't say anything. Do you get an error, does the file get uploaded, what does the resulting output look like? Start debugging, for example by simply putting `echo "File too large";` inside the second `else` block. Does it work without the file constraints? Any errors? Also, from the [manual](http://www.php.net/manual/en/features.file-upload.post-method.php), a file larger than `MAX_FILE_SIZE` will also result in `$_FILES['file']['size']` being 0, therefore not entering your outer `if`. – CodeCaster Jun 12 '12 at 08:29
  • @gyogyo0101 Are you sure that filesize does not equal 0? maybe you'd better add `else` at the end and take a look. – Leri Jun 12 '12 at 08:30
  • @CodeCaster I don't get an error. Files don't get uploaded (maybe because of upload_max_filesize = 3M in my php.ini – gyogyo0101 Jun 12 '12 at 08:35
  • _"I don't get an error"_ Then [enable error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php). – CodeCaster Jun 12 '12 at 08:37
  • add `var_dump($_FILES);` over the code to see if the first condition is true. – sofl Jun 12 '12 at 08:37
  • @PLB Still nothing. Posts get submitted without images attached. – gyogyo0101 Jun 12 '12 at 08:37
  • @CodeCaster done. No error related to above code. – gyogyo0101 Jun 12 '12 at 08:42
  • 1
    @gyogyo0101 Please do not edit title to mark situation solved, instead just mark answer as accepted. Also if you have found solution by yourself you can post it as answer to your own question. See [FAQ](http://stackoverflow.com/faq#howtoask) and [this meta question](http://meta.stackexchange.com/questions/86278). – Sampo Sarrala - codidact.org Jun 12 '12 at 09:26

1 Answers1

0

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.

verisimilitude
  • 5,077
  • 3
  • 30
  • 35