示例#1
0
 /**
  * Input file for media, using for upload file
  * @param File Object $fileObj File upload by user
  * @return boolean           true if all ok. Otherwise, false
  */
 public function initFile($fileObj)
 {
     $fileExt = $fileObj->getExtension();
     $filesAccept = MediaType::getExtensionAllowed();
     // Check if file extension's allowed
     if (!in_array($fileExt, $filesAccept)) {
         return $this->setError(MEDIA_FILE_NOT_ACCEPT . ": " . $fileExt);
     }
     // determine directory for this file. <username>/<file type>/<year>/<month>/<filename>
     $userName = $this->getDI()->getAuth()->getUsername();
     $year = date("Y");
     $month = date("M");
     $fileName = $fileObj->getName();
     $fileType = MediaType::getTypeFromExt($fileExt);
     // generate path of file
     $serverPath = $userName . DS . $fileType->getName() . DS . $year . DS . $month . DS . $fileName;
     $localPath = $fileObj->getTempName();
     if (!file_exists($localPath)) {
         return $this->setError(MEDIA_TEMP_NOT_FOUND);
     }
     if ($this->fileSystem->checkFileExists($serverPath)) {
         return $this->setError(MEDIA_ALREADY_EXISTS);
     }
     if (!$this->fileSystem->uploadFile($localPath, $serverPath)) {
         return $this->setError(MEDIA_TEMP_NOT_FOUND);
     }
     $uploadStatus = $this->saveToDB($userName, $fileType->getId(), time(), $fileName);
     if (!$uploadStatus) {
         return $this->setError(MEDIA_UPLOAD_ERROR);
     }
     // Update analytic file
     $config = $this->fileSystem->getConfigFile($userName);
     $defaultConfig = MediaType::getConfig();
     $config = array_merge($defaultConfig, $config);
     $config[$fileType->getName()]++;
     $this->fileSystem->saveConfigFile($userName, $config);
     if ($fileType->getName() == "Images") {
         $this->generateThumb($localPath, $serverPath);
     }
     return true;
 }