title: PHP Passing $_FILES and $_POST as parameters to function
body: <p>I searched online for a code that does PHP image upload using ajax. I found the code attached below. The issue is that I changed few things (minor tweaks) to make it work on my server. Originally it was just a php page (not a class or function) that handles the data posted from form. I made it into class then function. I am following OOP now. I thought the best way to do things in the conversion from procedural to OOP was to pass $_FILES and $_POST to a method and inside deal with them. I think this didn't work. Look at the example and please advise on how to go forward.</p>
  
  <pre><code>function uploadImageChosen($_FILES, $_POST){
              $path = "../uploads/images/";
              $valid_formats = array("jpg", "png", "gif", "bmp");
              $connectionInstance = new ConnectionClass();
              $connectionInstance-&gt;connectToDatabase();
              $imgName;
              $imgURL;
              $imgSize;
              $imgDir = $_POST['directory'];
              if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
              {
                  $name = $_FILES['photoimg']['name'];
                  $imgSize = $_FILES['photoimg']['size'];
                  if(strlen($name))
                  {
                      list($txt, $ext) = explode(".", $name);
                      if(in_array($ext,$valid_formats))
                      {
                          if($size&lt;(1024*1024))
                          {
                              $imgName = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
                              $tmp = $_FILES['photoimg']['tmp_name'];
                              if(move_uploaded_file($tmp, $path.$imgName))
                              {
                                  $imgURL = $path.$imgName;
                                  $connectionInstance-&gt;query("INSERT INTO imagesupload(id, title, url, size, directory) VALUES (null, '$imgName','$imgURL', '$imgSize', '$imgDir')");
                                  //echo "&lt;img src='uploads/".$imgName."'  class='preview'&gt;";
                              }
                              else{
                                  echo "failed";
                              }
                          }else{
                              echo "Image file size max 1 MB";                    
                          }
                      }else{
                          echo "Invalid file format..";   
                      }
              }else{
                  echo "Please select image..!";
              }           
  
            }//end of if      
  
          }//end of function
  </code></pre>
  
  <p>As to the page where the class function is being called, here it is:</p>
  
  <pre><code>&lt;?php
      require_once("../classes/UploadImages.php");
      $uploadInstance = new UploadImages();
      $uploadInstance-&gt;uploadImageChosen($_FILES, $_POST);
      //header("LOCATION:portfolio.php");
  ?&gt;
  </code></pre>
  
  <p>Thank you very much :)</p>