<?php
 
/**
 
 * Uploaded File Class
 
 *
 
 * @author    Emre Hasegeli
 
 */
 
 
require_once 'interface/File.php';
 
 
class UploadedFile implements iFile
 
{
 
    private $formInputName;
 
    protected $name;
 
    protected $extension;
 
    protected $size;
 
 
    public function __construct($formInputName, $name = NULL, $extension = NULL)
 
    {
 
        $this->formInputName = $formInputName;
 
 
        if(!$name)
 
        {
 
            $extension = substr($_FILES[$this->formInputName]['name'], strrpos($_FILES[$this->formInputName]['name'], '.') + 1);
 
 
            $name = substr($_FILES[$this->formInputName]['name'], 0, strrpos($_FILES[$this->formInputName]['name'], '.'));
 
        }
 
        if(!$extension)
 
        {
 
            $extension = substr($name, strrpos($name, '.') + 1);
 
 
            $name = substr($name, 0, strrpos($name, '.'));
 
        }
 
 
        $this->name = File::NameCheck($name);
 
 
        $this->extension = File::ExtensionCheck($extension);
 
 
        $this->size = $_FILES[$this->formInputName]['size'];
 
 
        $this->exception();
 
    }
 
 
    public function __destruct()
 
    {
 
        unset($_FILES[$this->formInputName]);
 
    }
 
 
    public function __toString()
 
    {
 
        return $this->name . '.' . $this->extension;
 
    }
 
 
    public function Name()
 
    {
 
        return $this->name;
 
    }
 
 
    public function Extension()
 
    {
 
        return $this->extension;
 
    }
 
 
    const minSize = 0;
 
 
    final private function exception()
 
    {
 
        if($_FILES[$this->formInputName]['error'])
 
            throw FileUploadException::Error($_FILES[$this->formInputName]['error']);
 
 
        $this->minSizeControl(); //After PHP 5.3 this can be handled in this function
 
 
        $this->typeException($_FILES[$this->formInputName]['type']);
 
    }
 
 
    protected function minSizeControl()
 
    {
 
        if($this->size <= self::minSize)
 
            throw new FileUploadException('The uploaded file is too small.');
 
    }
 
 
    protected function typeException($type)
 
    {
 
        if($type == 'application/octet-stream')
 
            throw new FileUploadException('The uploaded file is not an application.');
 
    }
 
 
    final public function Size()
 
    {
 
        return $this->size;
 
    }
 
 
    const object = 'File'; //After PHP 5.3 this can be written in interface
 
 
    public function Move($directory)
 
    {
 
        if(move_uploaded_file($_FILES[$this->formInputName]['tmp_name'], $directory . $this->name . '.' . $this->extension))
 
        {
 
            $object = self::object;
 
 
            return new $object($directory, $this->name, $this->extension);
 
 
            unset($this);
 
        }
 
        else
 
            throw new FileUploadException('The file can not moved.');
 
    }
 
 
    public function Rename($name)
 
    {
 
        $this->name = File::NameCheck($name);
 
    }
 
 
    public function Delete()
 
    {
 
        if(unlink($_FILES[$this->formInputName]['tmp_name']))
 
            unset($this);
 
        else
 
            throw new FileUploadException('The file can not deleted.');
 
    }
 
 
    public static function Select($formInputName)
 
    {
 
        if(is_uploaded_file($_FILES[$formInputName]['tmp_name']))
 
            return new self($formInputName, $name = NULL, $extension = NULL);
 
        else
 
            return NULL;
 
    }
 
} ?>
 
 |