title: how to display error message in image upload
body: <p>How do I display "file size is greater than the specified" and "Please upload only Image" message to user? I tried with the below code</p>
  
  <p><code>if ($_FILES["file"]["size"] &gt; 2097152) { // if file is larger than we want to allow
      echo "ERROR: Your file was larger than 2MB in file size.";</code> </p>
  
  <p>but that didn't work. 
      
      
      
      </p>
  
  <pre><code>&lt;form action="" method="post"
  enctype="multipart/form-data"&gt;
  &lt;label for="file"&gt;Filename:&lt;/label&gt;
  &lt;input type="file" name="file" &gt;&lt;br&gt;
  &lt;input type="submit" name="submit" value="Submit"&gt;
  &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt; 
  </code></pre>
  
  <p>Here is my php code below. It works by adding <code>else
     {
      echo "Invalid file";
      }</code> at the end but i wanted to show individual error to the user...txs</p>
  
  <pre><code>&lt;?php
  $allowedExts = array("jpg", "jpeg", "gif", "png");
  $extension = end(explode(".", $_FILES["file"]["name"]));
  if ((($_FILES["file"]["type"] == "image/gif")
  || ($_FILES["file"]["type"] == "image/jpeg")
  || ($_FILES["file"]["type"] == "image/png")
  || ($_FILES["file"]["type"] == "image/pjpeg"))
  &amp;&amp; ($_FILES["file"]["size"] &lt; 2097152)
  &amp;&amp; in_array($extension, $allowedExts))
  {
  
  if ($_FILES["file"]["error"] &gt; 0)
  {
  echo "Return Code: " . $_FILES["file"]["error"] . "&lt;br&gt;";
  }
  else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "&lt;br&gt;";
  echo "Type: " . $_FILES["file"]["type"] . "&lt;br&gt;";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB&lt;br&gt;";
  echo "Temp file: " . $_FILES["file"]["tmp_name"] . "&lt;br&gt;";
  
  if ($_FILES["file"]["size"] &gt; 2097152) { // if file is larger than we want to allow
  echo "ERROR: Your file was larger than 2MB in file size.";
  }
  if (file_exists("upload/" . $_FILES["file"]["name"]))
  {
  echo $_FILES["file"]["name"] . " already exists. ";
  }
  else
  {
  move_uploaded_file($_FILES["file"]["tmp_name"],
  "upload/" . $_FILES["file"]["name"]);
  echo "your photo has been uploaded successfully!";
  }
  }
  }
  ?&gt; 
  </code></pre>