Location: PHPKode > projects > Web2.0 community/group web site > web20groups_pre-alpha-2/include/Controllers/FileController.php
<?php
	
class FileController extends CommonCustomControllerAction
{
    const MAX_FILESIZE= 2000000; //2MB

    public function init()
    {
        parent::init();
        $this->view->extra_styles= array(
                                        );
        $this->view->extra_scripts= array(
                                         );
    }

	public function indexAction()
	{
	}

    public function uploadAction()
    {
        $logger= Zend_Registry::get('logger');

        $request= $this->getRequest();
        
        if ($request->isPost()) {
            if ((($_FILES["file"]["type"] == "image/gif")
                    || ($_FILES["file"]["type"] == "image/jpeg")
                    || ($_FILES["file"]["type"] == "image/pjpeg")
                    || ($_FILES["file"]["type"] == "image/png")
                    || ($_FILES["file"]["type"] == "image/bmp"))
                    && ($_FILES["file"]["size"] < self::MAX_FILESIZE)) {

                if ($_FILES["file"]["error"] > 0) {
                    $file['error']= "Error in uploading file";
                }
                else {
                    $dbfile= new DatabaseObject_File($this->db);

                    $file['name']= $_FILES["file"]["name"];
                    $file['type']= $_FILES["file"]["type"];
                    $file['size']= $_FILES["file"]["size"] / 1024; //Kb
                    $tmpfilepath= $_FILES["file"]["tmp_name"];

                    $dbfile->filename= $file['name'];
                    $dbfile->filetype= $file['type'];
                    $dbfile->size_kb= $file['size'];
                    $dbfile->user_id= $this->user_id;

                    if ($dbfile->save($tmpfilepath)) {
                        $uploadpath= Zend_Registry::get('uploadpath');

                        $prefix= str_ireplace($_SERVER['DOCUMENT_ROOT'],
                        '',$uploadpath);
                        $file['url']= '/' . trim($prefix, '/') . '/'. $dbfile->filename;
                        $file['name']= $dbfile->filename;
                        
                        $this->view->file= $file;
                    }
                }
            }
            else {
                $file['error']= "Invalid file (other than image files not
                    allowed";
            }
        }
    }

    public function deleteAction()
    {
        $logger= Zend_Registry::get('logger');
        $request= $this->getRequest();

        $this->param_url= $request->getParam('fileurl');

        if ($this->param_url != '') {
            $uploadpath= Zend_Registry::get('uploadpath');
            $prefix= str_ireplace($_SERVER['DOCUMENT_ROOT'],'',$uploadpath);
            $filename= str_ireplace($prefix,'',$this->param_url);
            $filename= trim($filename, '/');

            $dbfile= new DatabaseObject_File($this->db);
            
            if ($dbfile->load($filename, "filename")) {
                if ($dbfile->user_id == $this->user_id) {
                    $dbfile->delete();
                    $file['name']= $filename;
                    $this->view->file= $file;
                }
            }
        }
    }

    public function getAction()
    {
        $logger= Zend_Registry::get('logger');

        $request= $this->getRequest();

        $filename= $request->getParam('filename');

        $failuremsg= '';
        if ($filename != '' ) {
            $splitarray= split('/', $filename);
            if (count($splitarray) == 2) {
                $dir= $splitarray[0];
                $filename= $splitarray[1];

                if ($dir == 'upload') {
                    $uploadpath= Zend_Registry::get('uploadpath');
                    //readfile
                    $filepath= $uploadpath . "/". $filename;

                    if (file_exists($filepath)) {
                        $this->_helper->viewRenderer->setNoRender();
                        $tmparray= split('-', $filename);
                        $fextension= $tmparray[count($tmparray)-1];
                        $this->renderFile($filepath, $fextension);
                        return;
                    }
                    else {
                        $failuremsg= "File does not exist";
                    }
                }
            }
            else {
                $failuremsg= "Invalid file name";
            }
        }
        else {
            $failuremsg= "Invalid file name";
        }

        $this->view->filename= $filename;
        $this->view->failuremsg= $failuremsg;
    }

    private function renderFile($filepath, $file_extension)
    {
        switch ($file_extension) {
                case "pdf": $ctype="application/pdf"; break;
                case "exe": $ctype="application/octet-stream"; break;
                case "zip": $ctype="application/zip"; break;
                case "doc": $ctype="application/msword"; break;
                case "xls": $ctype="application/vnd.ms-excel"; break;
                case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
                case "gif": $ctype="image/gif"; break;
                case "png": $ctype="image/png"; break;
                case "jpe": case "jpeg":
                case "jpg": $ctype="image/jpg"; break;
                default: $ctype="application/force-download";
        }

        header('Content-Description: File Transfer');
        header('Content-Type: ' .$ctype);
//        header('Content-Disposition: attachment; filename='.basename($filepath).'.' .$file_extension);
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($filepath));
        ob_clean();
        flush();
        readfile($filepath);
        exit;
    }
}
?>
Return current item: Web2.0 community/group web site