Location: PHPKode > projects > ContentCMS > core/content_upload.class.php
<?php
/* 
 * @package     ContentCMS
 * @author      Dan Goldsmith
 * @copyright   Dan Goldsmith 2012
 * @link        http://contentcms.d2g.org.uk/
 * @version     {SUBVERSION_BUILD_NUMBER}
 * 
 * @licence     MPL 2.0
 * 
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 
 */
class content_upload
{
    private $content_id         = null;
    private $filename           = null;
    private $full_filename      = null;
    
    public function __construct($content_id,$filename)
    {
        $this->setContentID($content_id);
        $this->setFilename($filename);
        $this->setFullFilename(CONTENT_APPLICATION_ROOT . "/" . CONTENT_UPLOADS_FILE_LOCATION . "/" . $this->getContentID() . "/" . $this->getFilename());
    }
    
    private function setContentID($id)
    {
        $this->content_id = $id;
    }
    
    public function getContentID()
    {
        return $this->content_id;
    }
    
    private function setFilename($filename)
    {
        $this->filename = $filename;
    }
    
    public function getFilename()
    {
        return $this->filename;
    }
    
    public function setFullFilename($full_file_name)
    {
        $this->full_filename = $full_file_name;
    }
    
    public function getFullFilename()
    {
        return $this->full_filename;
    }
    
    
    
    
    
    
    //Move Tmp to Live
    public function setContentFromTemp($temp_filename,$overwrite = false)
    {
        if(!is_dir(CONTENT_APPLICATION_ROOT . "/" . CONTENT_UPLOADS_FILE_LOCATION . "/"))
        {
            throw new exception('Upload direcotory ' . CONTENT_UPLOADS_FILE_LOCATION . ' does not exist.');
        }
        
        if(!is_dir(CONTENT_APPLICATION_ROOT . "/" . CONTENT_UPLOADS_FILE_LOCATION . "/" . $this->getContentID() . "/"))
        {
            if(!mkdir(CONTENT_APPLICATION_ROOT . "/" . CONTENT_UPLOADS_FILE_LOCATION . "/" . $this->getContentID() . "/"))
            {
                throw new exception('Unable to create directory ' . CONTENT_APPLICATION_ROOT . "/" . CONTENT_UPLOADS_FILE_LOCATION . "/" . $this->getContentID() . "/");
            }
        }
        
        if(!is_dir(dirname(CONTENT_APPLICATION_ROOT . "/" . CONTENT_UPLOADS_FILE_LOCATION . "/" . $this->getContentID() . "/" . $this->getFilename())))
        {
            if(!mkdir(dirname(CONTENT_APPLICATION_ROOT . "/" . CONTENT_UPLOADS_FILE_LOCATION . "/" . $this->getContentID() . "/" . $this->getFilename()),0777,true))
            {
                throw new exception('Unable to create directory ' . dirname(CONTENT_APPLICATION_ROOT . "/" . CONTENT_UPLOADS_FILE_LOCATION . "/" . $this->getContentID() . "/" . $this->getFilename()));
            }
        }
        
        if(!$overwrite && is_file(CONTENT_APPLICATION_ROOT . "/" . CONTENT_UPLOADS_FILE_LOCATION . "/" . $this->getContentID() . "/" . $this->getFilename()))
        {
            throw new exception('File ' . $temp_filename, CONTENT_APPLICATION_ROOT . "/" . CONTENT_UPLOADS_FILE_LOCATION . "/" . $this->getContentID() . "/" . $this->getFilename() . ' already exists.');
        }
        
        if(!move_uploaded_file($temp_filename, CONTENT_APPLICATION_ROOT . "/" . CONTENT_UPLOADS_FILE_LOCATION . "/" . $this->getContentID() . "/" . $this->getFilename()))
        {
            throw new exception('Unable to move temp file ' . $temp_filename . ' to location ' . CONTENT_APPLICATION_ROOT . "/" . CONTENT_UPLOADS_FILE_LOCATION . "/" . $this->getContentID() . "/" . $this->getFilename());
        }
    }
    
    //Allow Script to send Correct Headers.
    public function getMimeType()
    {
        //Load the finfo patch
        require_once(CONTENT_LIBARIES_DIRECTORY . "patches/finfo.patch.class.php");
        
        $finfo = new finfo(FILEINFO_MIME);
        $type = $finfo->file($this->getFullFilename());
        return substr($type, 0, strpos($type, ';'));
    }
    
    public function streamContents()
    {
        $total_size = filesize($this->getFullFilename());
        $blocksize = (2 << 20); //2M chunks

        $sent = 0;
        $handle = fopen($this->getFullFilename(), "r");
                
        while($sent < $total_size)
        {    
            echo fread($handle, $blocksize);
            $sent += $blocksize;
        }
    
        fclose($handle);
    }


    
    public function delete()
    {
        unlink($this->getFullFilename());
    }
    
    
    
    public static function processRequest($content_id,$base_directory = null,$overwrite = false)
    {
        $uploaded_files = array();
        
        foreach($_FILES as $uploaded_key => $uploaded_values)
        {
            if(array_key_exists('name',$uploaded_values))
            {
                if(is_array($uploaded_values['name']))
                {
                    $uploads_for_key = array();
                    
                    foreach($uploaded_values['name'] as $id => $name)
                    {
                        if($uploaded_values['error'][$id] == UPLOAD_ERR_OK)
                        {
                            if($base_directory !== null)
                            {
                                $name = $base_directory . "/" . $name;
                            }
                            $uploaded_file = new self($content_id,$name);
                            $uploaded_file->setContentFromTemp($uploaded_values['tmp_name'][$id],$overwrite);

                            $uploads_for_key[$id] = $uploaded_file;                            
                        }
                        else
                        {
                            switch($uploaded_values['error'])
                            {
                                case UPLOAD_ERR_INI_SIZE:
                                    $uploaded_files[$uploaded_key] = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
                                break;
                                case UPLOAD_ERR_FORM_SIZE:
                                    $uploaded_files[$uploaded_key] = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
                                break;
                                case UPLOAD_ERR_PARTIAL:
                                    $uploaded_files[$uploaded_key] = 'The uploaded file was only partially uploaded';
                                break;
                                case UPLOAD_ERR_NO_FILE:
                                    $uploaded_files[$uploaded_key] = 'No file was uploaded';
                                break;
                                case UPLOAD_ERR_NO_TMP_DIR:
                                    $uploaded_files[$uploaded_key] = 'Missing a temporary folder';
                                break;
                                case UPLOAD_ERR_CANT_WRITE:
                                    $uploaded_files[$uploaded_key] = 'Failed to write file to disk';
                                break;
                                case UPLOAD_ERR_EXTENSION:
                                    $uploaded_files[$uploaded_key] = 'A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo() may help';
                                break;
                                default:
                                    $uploaded_files[$uploaded_key] = 'An Unnown Error code ' . $uploaded_values['error'] . ', please check: http://www.php.net/manual/en/features.file-upload.errors.php';
                                break;
                            }                            
                        }
                    }
                    
                    $uploaded_files[$uploaded_key] = $uploads_for_key;
                }
                else
                {
                    if($uploaded_values['error'] == UPLOAD_ERR_OK)
                    {
                        if($base_directory !== null)
                        {
                            $uploaded_values['name'] = $base_directory . "/" . $uploaded_values['name'];
                        }                        
                        $uploaded_file = new self($content_id,$uploaded_values['name']);
                        $uploaded_file->setContentFromTemp($uploaded_values['tmp_name'],$overwrite);
                        
                        $uploaded_files[$uploaded_key] = $uploaded_file;
                    }
                    else
                    {
                        switch($uploaded_values['error'])
                        {
                            case UPLOAD_ERR_INI_SIZE:
                                $uploaded_files[$uploaded_key] = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
                            break;
                            case UPLOAD_ERR_FORM_SIZE:
                                $uploaded_files[$uploaded_key] = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
                            break;
                            case UPLOAD_ERR_PARTIAL:
                                $uploaded_files[$uploaded_key] = 'The uploaded file was only partially uploaded';
                            break;
                            case UPLOAD_ERR_NO_FILE:
                                $uploaded_files[$uploaded_key] = 'No file was uploaded';
                            break;
                            case UPLOAD_ERR_NO_TMP_DIR:
                                $uploaded_files[$uploaded_key] = 'Missing a temporary folder';
                            break;
                            case UPLOAD_ERR_CANT_WRITE:
                                $uploaded_files[$uploaded_key] = 'Failed to write file to disk';
                            break;
                            case UPLOAD_ERR_EXTENSION:
                                $uploaded_files[$uploaded_key] = 'A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo() may help';
                            break;
                            default:
                                $uploaded_files[$uploaded_key] = 'An Unnown Error code ' . $uploaded_values['error'] . ', please check: http://www.php.net/manual/en/features.file-upload.errors.php';
                            break;
                        }
                    }                    
                }                
            }
        }
        
        return $uploaded_files;
    }    
    
    /*
     * Return all file that are in the directory for this content_id.
     */
    public static function getInDirectory($content_id,$directory = null)
    {
        if($directory !== null)
        {
            if(is_dir(CONTENT_APPLICATION_ROOT . "/" . CONTENT_UPLOADS_FILE_LOCATION . "/" . $content_id . "/" . $directory . "/"))
            {
                $directory_itterator = new DirectoryIterator(CONTENT_APPLICATION_ROOT . "/" . CONTENT_UPLOADS_FILE_LOCATION . "/" . $content_id . "/" . $directory . "/");
            }
            else
            {
                return array();
            }
        }
        else
        {
            if(is_dir(CONTENT_APPLICATION_ROOT . "/" . CONTENT_UPLOADS_FILE_LOCATION . "/" . $content_id . "/"))
            {
                $directory_itterator = new DirectoryIterator(CONTENT_APPLICATION_ROOT . "/" . CONTENT_UPLOADS_FILE_LOCATION . "/" . $content_id . "/");
            }
            else
            {
                return array();
            }            
        }
        
        $uploads = array();
        
        foreach ($directory_itterator as $file)
        {
            if ($file->isDot())
            {
                continue;
            }
        
            if($file->isDir())
            {
                if($directory !== null)
                {
                    $uploads = array_merge($uploads,self::getInDirectory($content_id,$directory . "/" . $file->getFilename()));
                }
                else
                {
                    $uploads = array_merge($uploads,self::getInDirectory($content_id,$file->getFilename()));
                }
            }
            
            if($file->isFile())
            {
                if($directory !== null)
                {
                    $uploads[] = new self($content_id,$directory . "/" . $file->getFilename());
                }
                else
                {
                    $uploads[] = new self($content_id,$file->getFilename());
                }   
            }
        }
        return $uploads;
    }
}

?>
Return current item: ContentCMS