Example #1
0
 public function __invoke($name = '')
 {
     if (empty($this->formFieldName) && !empty($name)) {
         $this->formFieldName = $name;
     }
     if (empty($this->formFieldName)) {
         throw new Exception('Empty form field name for file upload');
     }
     if (empty($this->uploadPath)) {
         throw new Exception('Invalid upload path');
     }
     $realUploadPath = \T4\Fs\Helpers::getRealPath($this->uploadPath);
     if (!is_dir($realUploadPath)) {
         try {
             \T4\Fs\Helpers::mkDir($realUploadPath);
         } catch (\T4\Fs\Exception $e) {
             throw new Exception($e->getMessage());
         }
     }
     $request = Application::getInstance()->request;
     if (!$request->isUploaded($this->formFieldName)) {
         throw new Exception('File for \'' . $this->formFieldName . '\' is not uploaded');
     }
     if (!$this->isUploaded($this->formFieldName)) {
         throw new Exception('Error while uploading file \'' . $this->formFieldName . '\': ' . $request->files->{$this->formFieldName}->error);
     }
     if ($request->isUploadedArray($this->formFieldName)) {
         $ret = [];
         foreach ($request->files->{$this->formFieldName} as $n => $file) {
             if (!$this->checkExtension($file->name)) {
                 throw new Exception('Invalid file extension');
             }
             $uploadedFileName = $this->suggestUploadedFileName($realUploadPath, $file->name);
             if (move_uploaded_file($file->tmp_name, $realUploadPath . DS . $uploadedFileName)) {
                 $ret[$n] = $this->uploadPath . '/' . $uploadedFileName;
             } else {
                 $ret[$n] = false;
             }
         }
         return $ret;
     } else {
         $file = $request->files->{$this->formFieldName};
         if (!$this->checkExtension($file->name)) {
             throw new Exception('Invalid file extension');
         }
         $uploadedFileName = $this->suggestUploadedFileName($realUploadPath, $file->name);
         if (move_uploaded_file($file->tmp_name, $realUploadPath . DS . $uploadedFileName)) {
             return $this->uploadPath . '/' . $uploadedFileName;
         } else {
             return false;
         }
     }
 }
Example #2
0
 public function actionAploadImg()
 {
     $realUploadPath = \T4\Fs\Helpers::getRealPath($this->app->request->post->path);
     if (!is_dir($realUploadPath)) {
         try {
             \T4\Fs\Helpers::mkDir($realUploadPath);
         } catch (\T4\Fs\Exception $e) {
             throw new Exception($e->getMessage());
         }
     }
     // Helpers::mkDir(ROOT_PATH_PUBLIC . DS .'/images/pages');
     $uploaddir = $realUploadPath;
     $file = $this->app->request->post->value;
     $name = $this->app->request->post->name;
     // Получаем расширение файла
     $getMime = explode('.', $name);
     $mime = end($getMime);
     // Выделим данные
     $data = explode(',', $file);
     // Декодируем данные, закодированные алгоритмом MIME base64
     $encodedData = str_replace(' ', '+', $data[1]);
     $decodedData = base64_decode($encodedData);
     var_dump($decodedData);
     var_dump($uploaddir . $name);
     // Создаем изображение на сервере
     if (file_put_contents($uploaddir . $name, $decodedData)) {
         echo $name . ":загружен успешно";
     } else {
         // Показать сообщение об ошибке, если что-то пойдет не так.
         echo "Что-то пошло не так. Убедитесь, что файл не поврежден!";
     }
     //$lin='__'.lcfirst($this->app->request->post->class).'_id';
     $item = new SiteImage();
     $subclass = $this->app->request->post->subclass;
     $namefield = $this->app->request->post->namefield;
     $item->{$namefield} = $this->app->request->post->path . $name;
     $item->published = date('Y-m-d H:i:s', time());
     $item->{$subclass} = $this->app->request->post->id;
     $item->save();
     die;
     //$this->redirect('admin/rubrics?id='.$this->app->request->post->pageid);
 }
Example #3
0
 public function addTemplatePath($path)
 {
     $this->paths[] = Helpers::getRealPath($path) ?: $path;
 }