ManiaLib 1.0b1 API Reference

Source for file ManialinkUploadHandler.class.php

Documentation is available at ManialinkUploadHandler.class.php

  1. <?php
  2. /**
  3.  * @author Maxime Raoust
  4.  * @copyright 2009-2010 NADEO
  5.  * @package ManiaLib
  6.  */
  7.  
  8. /** 
  9.  * Helps dealing with uploads from Manialinks
  10.  * @package ManiaLib
  11.  * @subpackage Helpers
  12.  */ 
  13. {
  14.     /**#@+
  15.      * @ignore
  16.      */
  17.     const READ_PACKET_SIZE 8192;
  18.     const UPLOADED_FILE_RIGHTS 0770;
  19.     protected $uploadPath;
  20.     protected $maxSize;
  21.     protected $uploadedFilename;
  22.     protected $inputFile;
  23.     /**#@-*/
  24.     
  25.     /**
  26.      * @param string The path where the file will be saved
  27.      * @param string The maximum file size in bytes
  28.      */
  29.     function __construct($uploadPath$maxSize)
  30.     {
  31.         $this->uploadPath $uploadPath;
  32.         $this->maxSize $maxSize;
  33.     }
  34.     
  35.     /**
  36.      * Tries to read the specified file and save it
  37.      * @param string 
  38.      */
  39.     function uploadFile($filename)
  40.     {
  41.         $this->uploadedFilename $filename;
  42.         
  43.         // Try to get POST data
  44.         $inputStream fopen('php://input''r');
  45.         $this->inputFile '' ;
  46.         $streamSize 0;
  47.         
  48.         while(!feof($inputStream))
  49.         {
  50.             $this->inputFile .= fread($inputStreamself::READ_PACKET_SIZE);
  51.             $streamSize += self::READ_PACKET_SIZE;
  52.             if($streamSize $this->maxSize)
  53.             {
  54.                 fclose($inputStream);
  55.                 unset($this->inputFile);
  56.                 throw new ManialinkUploadFileTooLargeException;
  57.             }
  58.         }
  59.         fclose($inputStream);
  60.  
  61.         // Else try to get GET data
  62.           if($this->inputFile=='' && array_key_exists('input'$_GET)) 
  63.           {
  64.             $this->inputFile $_GET['input'];
  65.           }
  66.           
  67.           // Check for error
  68.           if($this->inputFile=='')
  69.           {
  70.               throw new ManialinkUploadException(
  71.                 'Couldn\'t read input file');
  72.           }
  73.           if(!file_put_contents(
  74.             $this->uploadPath.$this->uploadedFilename
  75.             $this->inputFile))
  76.         {
  77.             throw new ManialinkUploadException(
  78.                 'Couldn\'t save input file to '.
  79.                 $this->uploadPath.$this->uploadedFilename);
  80.         }
  81.         if(!chmod($this->uploadPath.$this->uploadedFilename
  82.             self::UPLOADED_FILE_RIGHTS))
  83.         {
  84.             throw new ManialinkUploadException(
  85.                 'Couldn\'t chmod input file at '.
  86.                 $this->uploadPath.$this->uploadedFilename);    
  87.         }
  88.     }
  89.     
  90.     /**
  91.      * Deletes the previously uploaded file
  92.      */
  93.     function deleteUploadedFile()
  94.     {
  95.         unlink($this->getUploadedFilename());
  96.     }
  97.     
  98.     /**
  99.      * Returns the complete uploaded file name
  100.      * @return string 
  101.      */
  102.     function getUploadedFilename()
  103.     {
  104.         return $this->uploadPath.$this->uploadedFilename;
  105.     }
  106.     
  107.     /**
  108.      * Returns the size of the uploaded file
  109.      */
  110.     function getUploadedFilesize()
  111.     {
  112.         clearstatcache();
  113.         return filesize($this->getUploadedFilename());
  114.     }
  115. }
  116.  
  117. /**
  118.  * @package ManiaLib
  119.  * @subpackage Helpers
  120.  * @ignore
  121.  */
  122. class ManialinkUploadException extends Exception {}
  123. /**
  124.  * @package ManiaLib
  125.  * @subpackage Helpers
  126.  * @ignore
  127.  */
  128. class ManialinkUploadFileTooLargeException extends ManialinkUploadException {}
  129.  
  130. ?>