Example #1
0
 /**
  * upload
  * this method allows uploading one or more than one file
  * @param  [object] $files catains all the file information
  * @return [array] returns an array with the file(s) uploaded
  */
 public function upload($files)
 {
     //checking if it's just one file
     if ($files instanceof UploadedFile) {
         $many = false;
         //more than one file
     } elseif (is_array($files)) {
         $many = true;
         //there was no files selected
     } else {
         return '';
     }
     //one file validation
     if (!$many) {
         $files = [$files];
     }
     /**
      * $uploaded
      * It is the array that will contains all the uploaded files information
      * @var array
      */
     $uploaded = [];
     foreach ($files as $file) {
         $info = (object) pathinfo(strtolower($file->getClientOriginalName()));
         $options = (object) $this->options;
         //setting file path
         $path = [storage_path(), self::$default_path, $options->path];
         if (@$options->code && \Auth::check()) {
             $path[] = \Auth::id();
         }
         //user folder
         if (@$options->subpath) {
             $path[] = $options->subpath;
         }
         //file type validation - if file type or any file type are allowed
         if ((!isset($options->valid) || preg_match($options->valid, '.' . $info->extension)) && $file->isValid()) {
             //subfolder
             $path = implode('/', $path);
             //destiny file
             $file_destiny = md5(time()) . '.' . $info->extension;
             //folder validation - if there is not folder, it will be created
             if (!is_dir($path)) {
                 mkdir($path, 0777, true);
             }
             //uploading file
             $return = $file->move($path, $file_destiny);
             //normalization of the file sent
             $this->normalice("{$path}/{$file_destiny}");
             //keeping the uploaded file path
             $uploaded[] = explode(self::$default_path, str_replace('\\', '/', $return))[1];
         } else {
             $MaxFilesize = File::formatBytes($file->getMaxFilesize());
             $uploaded[] = "Error: " . trans('globals.file_upload_error', ['MaxFilesize' => $MaxFilesize]);
         }
     }
     return $many ? $uploaded : $uploaded[0];
 }
Example #2
0
 /**
  * Sube uno o varios archivos
  * @param  String $files    archivo(s) a subir
  * @return String/array     Url o array de urls de archivo(s) subido(s)
  */
 public function upload($files)
 {
     if ($files instanceof UploadedFile) {
         $many = false;
     } elseif (is_array($files)) {
         $many = true;
     } else {
         return '';
     }
     if (!$many) {
         $files = [$files];
     }
     $uploaded = [];
     foreach ($files as $file) {
         $info = (object) pathinfo($file->getClientOriginalName());
         $options = (object) $this->options;
         #Se configura el path con los datos pasados
         $path = [storage_path(), self::$default_path, $options->path];
         if (@$options->code && \Auth::check()) {
             $path[] = \Auth::id();
         }
         //Carpeta de usuario
         if (@$options->subpath) {
             $path[] = $options->subpath;
         }
         #validamos si se permite cualquier archivo o si el tipo de archivo esta permitido
         if ((!isset($options->valid) || preg_match($options->valid, '.' . $info->extension)) && $file->isValid()) {
             //subcarpeta
             $path = implode('/', $path);
             #archivo destino
             $file_destiny = md5(time()) . '.' . $info->extension;
             #Si no existe directorio se crea
             if (!is_dir($path)) {
                 mkdir($path, 0777, true);
             }
             #Se sube la imagen
             $return = $file->move($path, $file_destiny);
             $this->normalice("{$path}/{$file_destiny}");
             #guarda la ruta de la imagen subida
             $uploaded[] = explode(self::$default_path, str_replace('\\', '/', $return))[1];
         } else {
             $MaxFilesize = File::formatBytes($file->getMaxFilesize());
             $uploaded[] = "Error: " . trans('globals.file_upload_error', ['MaxFilesize' => $MaxFilesize]);
         }
     }
     return $many ? $uploaded : $uploaded[0];
 }