Source code for /Users/Shared/www/vhosts/top-frog/public_html/script_src/files/Upload.class.php

  1.  <?php
  2.   /**
  3.   * Basic file uploader
  4.   *
  5.   * @example
  6.   * $ul = new AP_File_Uploader();
  7.   * $upload_location = $ul->save_file($_FILES['myfile'],$destination,'my_new_file.jpg');
  8.   * if(!upload_location)
  9.   * {
  10.   * ... handle error ...
  11.   * $error = $ul->get_error();
  12.   * }
  13.   *
  14.   * @uses AP_File_MimeTypes
  15.   *
  16.   */
  17.   class AP_File_Upload
  18.   {
  19.   /**
  20.   * list of acceptable file types
  21.   *
  22.   * @var array
  23.   */
  24.   public $accept;
  25.  
  26.   /**
  27.   * file destination
  28.   *
  29.   * @var string - server path
  30.   */
  31.   public $destination;
  32.  
  33.   /**
  34.   * Error string
  35.   * If anything goes wrong this will give the reason
  36.   *
  37.   * @var string
  38.   */
  39.   private $_error;
  40.  
  41.   /**
  42.   * constuct
  43.   *
  44.   * @param string $destination
  45.   * @param array $accept
  46.   */
  47.   public function __construct($accept=false)
  48.   {
  49.   if(is_array($accept)) { $this->accept = $accept; }
  50.   else
  51.   {
  52.   if(class_exists('AP_File_MimeTypes'))
  53.   {
  54.   $mt = new AP_File_MimeTypes();
  55.   $this->accept = $mt->getTypes();
  56.   unset($mt);
  57.   }
  58.   else { throw new Exception('Mime type class not defined'); }
  59.   }
  60.   }
  61.  
  62.   /**
  63.   * Check file type
  64.   *
  65.   * @param string $file
  66.   * @return bool
  67.   */
  68.   public function check_type($file)
  69.   {
  70.   // get file type
  71.   $ext = pathinfo($file['name'],PATHINFO_EXTENSION);
  72.  
  73.   // make sure the image is a valid file type and that they type matches the extention
  74.   if(!array_key_exists($ext,$this->accept))
  75.   {
  76.   $this->_error = 'invalid file type';
  77.   return false;
  78.   }
  79.   elseif($this->accept[$ext] != $file['type'])
  80.   {
  81.   $this->_error = 'file type does not match file extention';
  82.   return false;
  83.   }
  84.   else { return true; }
  85.   }
  86.  
  87.   /**
  88.   * Make sure uploaded file exists
  89.   *
  90.   * @param string $path
  91.   * @return bool
  92.   */
  93.   public function check_temp($path)
  94.   {
  95.   if(is_file($path) && is_uploaded_file($path)) { return true; }
  96.   else
  97.   {
  98.   $this->_error = 'could not find uploaded file';
  99.   return false;
  100.   }
  101.   }
  102.  
  103.   /**
  104.   * Return the error string
  105.   *
  106.   * @return string
  107.   */
  108.   public function get_error()
  109.   {
  110.   return $this->_error;
  111.   }
  112.  
  113.   /**
  114.   * Save uploaded file
  115.   *
  116.   * $file = $_FILE['upload_name'];
  117.   *
  118.   * @param array $file - POST $_FILES array for file
  119.   * @param string $destination - where to save the file
  120.   * @param bool/string $new_name - false or what to rename the file
  121.   * @return bool/string - false or location of uploaded file
  122.   */
  123.   public function save($file,$destination,$new_name=false)
  124.   {
  125.   switch($file['error'])
  126.   {
  127.   case 0:
  128.   // process destination
  129.   if(is_dir($destination))
  130.   {
  131.   if(is_writable($destination)) { $this->destination = $destination; }
  132.   else { throw new Exception('Destination is not writable - check directory permissions.'); }
  133.   }
  134.   else { throw new Exception('Provided directory for file upload is not valid'); }
  135.  
  136.   // make sure a file was uploaded
  137.   if($this->check_temp($file['tmp_name']) && $this->check_type($file))
  138.   {
  139.   // use original file name if a new one is not supplied, replace spaces with underscores
  140.   if(!$new_name) { $new_name = str_replace(' ','_',$file['name']); }
  141.  
  142.   // build full file path for move
  143.   $uploadfile = $destination.$new_name;
  144.  
  145.   // if we're looking at an uploaded file and the file
  146.   // can be moved then we're OK
  147.   if(!move_uploaded_file($file['tmp_name'], $uploadfile))
  148.   {
  149.   $this->_error = 'Could not move uploaded file - flog the webmaster';
  150.   return false;
  151.   }
  152.   else
  153.   {
  154.   // change permissions on the file
  155.   if(chmod($uploadfile,0755)) { return $uploadfile; }
  156.   else
  157.   {
  158.   $this->_error = 'Could not change permissions on uploaded file. '.
  159.   'Please contact the system administrator';
  160.   return false;
  161.   }
  162.   }
  163.   }
  164.   else { return false; }
  165.   case 1:
  166.   case 2:
  167.   $this->_error = 'Your file was too large. Please edit your file or upload a different file.';
  168.   return false;
  169.   case 3:
  170.   $this->_error = 'File upload incomplete - Please retry your upload';
  171.   return false;
  172.   case 4:
  173.   $this->_error = 'No file uploaded. Please select a file to upload';
  174.   return false;
  175.   case 6:
  176.   $this->_error = 'No temp folder on server - whack the server admin';
  177.   return false;
  178.   case 7:
  179.   $this->_error = 'Failed to write to disk';
  180.   return false;
  181.   default:
  182.   $this->_error = 'Unknown error occured';
  183.   return false;
  184.   } // switch
  185.   }
  186.  
  187.   }
  188.  ?>