topical media & game development

talk show tell print

professional-sql-06-public-files-upload.php / php



  <?php
  include '../lib/common.php';
  include '../lib/config.php';
  
  // make sure we have all expected parameters
  if (!isset(_POST['directory'])) return;
  
  // the file uploaded successfully
  if (!_FILES['file']['error'])
  {
      // prevent users from traversing outside the base directory
      dir = realpath(BASEDIR . _POST['dir']);
      target = BASEDIR . dir . '/' . _FILES['file']['name'];
      if (strpos(target, BASEDIR) !== 0)
      {
          echo '<script type="text/javascript">parent.uploadFailed();</script>';
          die();
      }
  
      // must move the file to a permanent location
      if (move_uploaded_file(_FILES['file']['tmp_name'], target))
      {
          echo '<script type="text/javascript">parent.refreshFilesList();' .
              '</script>';
      }
      else
      {
          // there was a problem moving the file
          echo '<script type="text/javascript">parent.uploadFailed();</script>';
      }
  }
  // there was a problem uploading the file
  else
  {
      echo '<script type="text/javascript">parent.uploadFailed();</script>';
  }
  ?>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.