title: Image upload script is not working
body: <p>I have a page called addbanner.php which contains HTML and PHP script for uploading image.
  but it is not working i m giving.</p>
  
  <pre><code>*********************************addbanner.php***************************************
     &lt;?php
     session_start();
  
  require_once'session.php';
  require_once '../cms/config.php';
  
  if(isset($_POST['submit']))
  {
  
  //for image upload part
  
  // define the posted file into variables
  //$bannerid=$_POST['bannerid'];
  
   $name = $_FILES['file']['name'];
   $tmp_name = $_FILES['file']['tmp_name'];
   $type = $_FILES['file']['type'];
   $size = $_FILES['file']['size'];
   $desc=$_POST['description'];
   $dir='uploads/';
   $allowedExts = array("jpg", "jpeg", "gif", "png");
   $bits = (explode(".",$name ));
   $extension=end($bits);
   if ((($type == "image/gif")
   || ($type == "image/jpeg")
   || ($type == "image/png")
   || ($type == "image/pjpeg"))
    &amp;&amp; ($size &lt; 20000)
   &amp;&amp; in_array($extension, $allowedExts))
   {
  if ($_FILES["file"]["error"] &gt; 0)
   {
    echo "Return Code: " . $_FILES["file"]["error"] . "&lt;br /&gt;";
    }
   else
    {
  
  
  
  
   //connecting the database
    $object=new connection();
    $object=$object-&gt;dbConnect();
    $st=$object-&gt;prepare("select * from banners where  file=:name");
    //$st-&gt;bindParam(1,$bannerid);
     $st-&gt;bindValue(":name",$name,PDO::PARAM_STR);
    $st-&gt;execute();
    if($st-&gt;rowCount()&gt;1)
    {
     echo "Error: Banner with this name is already exists!";
     }
      else
      {
  
     //$object=new connection();
    //$object=$object-&gt;dbConnect();
     $st=$object-&gt;prepare("insert into banners values('$name','$type','$size','$desc')");
     $st-&gt;execute();
       if (file_exists("uploads/" . $name))
           {
       echo $name. " already exists. ";
        }
       else
        {
          move_uploaded_file($tmp_name,$dir.$name);
             echo "Stored in: " . "uploads/" . $name;
          }
            }
        }
               }
        else
            {
               echo "Invalid file";
            }
                }
        else
             {
               //HTML part
             }
           ?&gt;
  ********************javascript part***************
  function validate()
  {
  if(document.myform.file.value=='' || document.myform.description.value=='')
  
  {
  alert("Please fill all the field!");
  
  return false;
  
  
  }
  
    }
  ******************end javascript***************
  
  *******************html form*******************
  </code></pre>
  
  
  
  
  
          Banner image(Required Field)
           
           </p>
                   <p>
  
                                         Description(Required Field)
                                       
                                      
                   </p>   
  
                        
  
  <pre><code>            &lt;!-- End Form --&gt;
  
                      &lt;!-- Form Buttons --&gt;
                      &lt;div class="buttons" &gt;
                        &lt;input type="submit" name="submit" class="button" value="Submit" /&gt;
                        &lt;input type="submit" name="cancel" class="button" value="Cancel" /&gt;
                      &lt;/div&gt;
                      &lt;!-- End Form Buttons --&gt;
                  &lt;/form&gt;
  
  
  ********************end coding*****************
  </code></pre>
  
  <p>when i choose submit buttton it prints "invalid file" which is else part of script.
  And there is also a problem in my javascript when i click submit leaving blank description field. It does not show alert sometimes. Please suggest something.
  I simply want when user fill both the field and click submit, it will go to database and also save in a given folder.</p>
  
  <p>And sorry for the incomplete script, I didn't code for cancel button yet.</p>