title: PHP image upload function, save in a dir and then return save image url
body: <p>I am trying to upload an image to server using PHP and the save inside a dir, and then returning the image url.</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;input name="photo" type="file" /&gt;
  </code></pre>
  
  <p>PHP</p>
  
  <pre><code>save_string_to_database( upload_img($_POST['photo']));
  </code></pre>
  
  <p>I have not much idea of PHP, I got a code from SO, but it did not do anything. Kindly help me to fix this code, or give a simple code to perform an upload:</p>
  
  <pre><code>function upload_img($img){
      if ((($_FILES[$img]["type"] == "image/gif")
      || ($_FILES[$img]["type"] == "image/jpeg")
      || ($_FILES[$img]["type"] == "image/pjpeg")
      || ($_FILES[$img]["type"] == "image/jpg")
      || ($_FILES[$img]["type"] == "image/png"))
      &amp;&amp; ($_FILES[$img]["size"] &lt; 20000)
      &amp;&amp; (strlen($_FILES[$img]["name"]) &lt; 51)){
         if ($_FILES[$img]["error"] &gt; 0){
             echo "Return Code: " . $_FILES[$img]["error"];
         }
         else{
             // echo "Upload: " . $_FILES["image"]["name"] . "&lt;br /&gt;";
             // echo "Type: " . $_FILES["image"]["type"] . "&lt;br /&gt;";
             // echo "Size: " . ($_FILES["image"]["size"] / 1024) . " Kb&lt;br /&gt;";
             //  echo "Temp file: " . $_FILES["image"]["tmp_name"] . "&lt;br /&gt;";
  
             if (file_exists(THEME_DIR."/images/" . $_FILES[$img]["name"])){
                 echo $_FILES[$img]["name"] . " already exists. ";
             }
             else{
                 move_uploaded_file($_FILES[$img]["tmp_name"],THEME_DIR."/images/"  . $_FILES[$img]["name"]);
                 return THEME_DIR."/images/"  . $_FILES[$img]["name"];
             }
         }
     }
  }
  </code></pre>