title: upload a file using php script without leaving that page
body: <p>I am trying to upload a file in html using php script.</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
    &lt;form id='importPfForm' enctype="multipart/form-data" action="http://localhost/js/upload.php" method="post"&gt;
      &lt;input type="hidden" name="MAX_FILE_SIZE" value="1000000" /&gt;
      Choose a file to upload: &lt;input name="uploaded_file" type="file" /&gt;
      &lt;input type="submit" value="Upload" /&gt;
    &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>and the "upload.php" contains the following code:</p>
  
  <pre><code>&lt;?php
  $upload_key = 'uploaded_file';
  if (isset($_FILES[$upload_key])) {
      try {
          $error = $_FILES[$upload_key]['error'];
          switch ($error) {
              case UPLOAD_ERR_INI_SIZE:
                  throw new Exception('Exceeded upload_max_filesize');
              case UPLOAD_ERR_FORM_SIZE:
                  throw new Exception('Exceeded MAX_FILE_SIZE');
              case UPLOAD_ERR_PARTIAL:
                  throw new Exception('Incomplete file uploaded');
              case UPLOAD_ERR_NO_FILE:
                  throw new Exception('No file uploaded');
              case UPLOAD_ERR_NO_TMP_DIR:
                  throw new Exception('No tmp directory');
              case UPLOAD_ERR_CANT_WRITE:
                  throw new Exception('Can\'t write data');
              case UPLOAD_ERR_EXTENSION:
                  throw new Exception('Extension error');
          }
          $finfo    = new finfo(FILEINFO_MIME);
          $name     = $_FILES[$upload_key]['name'];
          $tmp_name = $_FILES[$upload_key]['tmp_name'];
          $size     = $_FILES[$upload_key]['size'];
          if ($size &gt; 350000)
              throw new Exception('Exceeded 350KB limit');
          if (!is_uploaded_file($tmp_name))
              throw new Exception('Not an uploaded file');
          $type = $finfo-&gt;file($tmp_name);
          if ($type === false)
              throw new Exception('Failed to get MimeType');
          if ($type !== 'text/plain; charset=us-ascii')
              throw new Exception('Only csv available');
          $new_name = dirname(__FILE__).'/upload/'.$name;
      echo  $new_name;
          if (is_file($new_name))
              throw new Exception("The file {$new_name} already exists");
          if (!move_uploaded_file($tmp_name,$new_name))
              throw new Exception('Failed to move uploaded file');
          echo "File successfully uploaded as {$new_name}";
      } catch (Exception $e) {
          echo 'Error: '.$e-&gt;getMessage();
      }
  }
  ?&gt;
  </code></pre>
  
  <p>But this method opens a new web page. I want to perform the function without leaving the web page and i need to use the variable  <code>$new_name</code> in html page. What is the modification i need to perform in my html page? I'm pretty sure this works via some kind of ajax request thing. but I have no idea what I'm talking about. I'm not big on ajax or javascript, but this is a function i use very frequently and I'd like to learn how it works so I can implement it when i need to now and in the future.</p>