コード例 #1
0
ファイル: FileUploader.php プロジェクト: Eximagen/sochi
 /**
  * Guarda el archivo
  * @param string $path El directorio donde deseamos que se guarde nuestro archivo
  * @param string $resize Hace una copia miniatura del archivo si es una imagen y deja el original con un sufijo ' _o '
  * @return boolean
  * @throws Exception
  */
 public function saveFile($path, $resize = true)
 {
     if (!is_dir($path . $this->path)) {
         if (!@mkdir($path . $this->path, 0777, true)) {
             throw new FileException('No se pudo crear el directorio destino, Compruebe que tiene los permisos suficientes en ' . $path . '');
         }
     }
     $name = $this->cleanName($this->originalName) . substr(md5($path . time()), 0, 5) . ($resize ? '_o' : '');
     //if a custom name is set it will be saved instead of the original
     if ($this->fileName == '') {
         $this->fileName = $name . '.' . $this->getFileExtension();
     } else {
         $this->fileName .= '.' . $this->getFileExtension();
     }
     $file = $path . $this->path . '/' . $this->fileName;
     if (!move_uploaded_file($_FILES[$this->name]['tmp_name'], $file)) {
         throw new FileException('El archivo no se pudo mover a su destino');
     }
     chmod($file, 0777);
     if ($resize && in_array($this->mimeType, $this->mimeImages)) {
         $imageResize = new ImageResizer($file, str_replace('_o', '', $file), 360, 480);
         $imageResize->getResizedImage();
         $this->fileName = str_replace('_o', '', $this->fileName);
         $imageResize = new ImageResizer($file, str_replace('_o', '_mini', $file), 120, 120);
         $imageResize->getResizedImage();
         $this->fileName = str_replace('_o', '', $this->fileName);
     }
     return true;
 }