Created
July 29, 2011 18:35
-
-
Save junichi11/1114423 to your computer and use it in GitHub Desktop.
CakePHP Plupload Component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* CakePHP Plupload Plugin | |
* Pluplaod Component | |
* | |
* CakePHP version 1.3+ | |
* PHP version 5.3+ | |
* | |
* @author junichi11 | |
* | |
*/ | |
class PluploadComponent extends Object{ | |
public $components = array('Session', 'RequestHandler'); | |
//=============================================== | |
// original property | |
//=============================================== | |
/** | |
* @var File | |
*/ | |
public $file = null; | |
public $settings = array( | |
'upload_dir' => "", | |
'time_limit' => 300, | |
); | |
protected $_controller = null; | |
protected $_chunk = 0; | |
protected $_chunks = 0; | |
//=============================================== | |
// callback method | |
//=============================================== | |
public function initialize(&$controller, $settings){ | |
$this->_controller = $controller; | |
$this->settings['upload_dir'] = WWW_ROOT. "pluploads"; | |
if($controller->name == 'Plupload'){ | |
$session = $this->Session->read('Pluplaod.Upload'); | |
if(!empty($session)){ | |
$this->settings = am($this->settings, $session); | |
$this->Session->delete('Plupload.Upload'); | |
} | |
}else{ | |
$this->Session->write('Pluplaod.Upload', $settings); | |
} | |
} | |
public function startup(&$controller) { | |
extract($this->settings); | |
$this->setTimeLimit($time_limit); | |
if(isset($controller->params['form']['chunk'])){ | |
$this->_chunk = (int)$controller->params['form']['chunk']; | |
} | |
if(isset($controller->params['form']['chunks'])){ | |
$this->_chunks = (int)$controller->params['form']['chunks']; | |
} | |
$this->setFile($upload_dir); | |
} | |
//=============================================== | |
// original method | |
//=============================================== | |
/** | |
* File upload | |
*/ | |
public function upload() { | |
if ($this->isMultipart()) { | |
$this->_multipartUpload(); | |
} else { | |
$this->_streamUpload(); | |
} | |
if ($this->isLastChunk()) { | |
$this->Session->setFlash('Upload complete.'); | |
} else { | |
die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}'); | |
} | |
} | |
/** | |
* Check last chunk | |
* @return boolean | |
*/ | |
public function isLastChunk(){ | |
if($this->_chunk == ($this->_chunks - 1)){ | |
return true; | |
} | |
return false; | |
} | |
/** | |
* isMultipart | |
* @return boolean | |
*/ | |
public function isMultipart(){ | |
return $this->RequestHandler->requestedWith('file'); | |
} | |
/** | |
* setUploadInfo | |
* @param array $data | |
* @return type | |
*/ | |
public function setUploadInfo($data = null){ | |
if($data == null){ | |
return false; | |
} | |
$this->Session->write('Pluplaod.Upload', $data); | |
} | |
/** | |
* setTimeLimit | |
* @param int $timeLimit | |
*/ | |
public function setTimeLimit($time_limit){ | |
set_time_limit($time_limit); | |
} | |
/** | |
* setChunk | |
* | |
*/ | |
private function setChunk(){ | |
$this->_chunk = isset($_REQUEST["chunk"]) ? $_REQUEST["chunk"] : 0; | |
} | |
/** | |
* setChunks | |
* | |
*/ | |
private function setChunks(){ | |
$this->_chunks = isset($_REQUEST["chunks"]) ? $_REQUEST["chunks"] : 0; | |
} | |
/** | |
* setFile | |
* Set upload directry & filename | |
* @param type $upload_dir upload directry | |
*/ | |
private function setFile($upload_dir){ | |
$fileName = isset($_REQUEST["name"]) ? $_REQUEST["name"] : ''; | |
$this->file = new File($upload_dir . DS . $fileName); | |
$this->file->Folder()->create($upload_dir); | |
$this->_createUniqueFile(); | |
} | |
/** | |
* getTargetDir | |
* Get upload directry | |
* @return string upload directry path | |
*/ | |
public function getTargetDir(){ | |
return $this->file->Folder()->pwd(); | |
} | |
/** | |
* setTargetDir | |
* Set upload directry | |
* @param string $upload_dir upload directry | |
*/ | |
public function setTargetDir($upload_dir){ | |
$this->file->Folder()->cd($upload_dir); | |
} | |
/** | |
* createUniqueFile | |
* Create unique filename | |
*/ | |
private function _createUniqueFile(){ | |
if ($this->_chunks < 2 && $this->file->exists()) { | |
$fileName = $this->file->name(); | |
$ext = $this->file->ext(); | |
$upload_dir = $this->getTargetDir(); | |
$count = 1; | |
while (file_exists($upload_dir . DS . $fileName . '_' . $count .'.'. $ext)){ | |
$count++; | |
} | |
$fileName = $fileName . '_' . $count . '.'.$ext; | |
$this->file = null; | |
$this->file = new File($upload_dir . DS . $fileName); | |
} | |
} | |
/** | |
* _multipartUpload | |
* Multipart upload | |
*/ | |
protected function _multipartUpload() { | |
if (isset($_FILES['file']['tmp_name']) && is_uploaded_file($_FILES['file']['tmp_name'])) { | |
$saveAs = $this->file->open($this->_chunk == 0 ? "wb" : "ab"); | |
if ($saveAs) { | |
$tmp = new File($_FILES['file']['tmp_name']); | |
$fp = $tmp->open("rb"); | |
if ($fp) { | |
while ($buff = $tmp->read(4096)){ | |
$this->file->write ($buff); | |
} | |
} else { | |
$this->Session->setFlash('Failed to open input stream. : '.$_FILES['file']['name'], 'default', array(), 'error'); | |
die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}'); | |
} | |
$tmp->close(); | |
} else{ | |
$this->Session->setFlash('Failed to open output stream. : '.$_FILES['file']['name'], 'default', array(), 'error'); | |
die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}'); | |
} | |
} else{ | |
$this->Session->setFlash('Failed to move uploaded file. : '.$_FILES['file']['name'], 'default', array(), 'error'); | |
die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}'); | |
} | |
} | |
/** | |
* _streamUpload | |
* Stream upload | |
*/ | |
protected function _streamUpload() { | |
$saveAs = $this->file->open($this->_chunk == 0 ? "wb" : "ab"); | |
if ($saveAs) { | |
$tmp = new File("php://input"); | |
$fp = $tmp->open("rb"); | |
if ($fp) { | |
while ($buff = $tmp->read(4096)){ | |
$this->file->write ($buff); | |
} | |
} else{ | |
$this->Session->setFlash('Failed to open input stream. : '.$_FILES['file']['name'], 'default', array(), 'error'); | |
die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}'); | |
} | |
$tmp->close(); | |
} else{ | |
$this->Session->setFlash('Failed to open output stream. : '.$_FILES['file']['name'], 'default', array(), 'error'); | |
die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}'); | |
} | |
} | |
/** | |
* setUploaderOptions | |
* @param array $options uploader's option | |
* @return boolean | |
*/ | |
public function setUploaderOptions($options = array()){ | |
if(!empty($options)){ | |
$this->Session->write('Plupload.Uploader', $options); | |
return true; | |
} | |
return false; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment