Beispiel #1
0
 /**
  * Закачивает файл `$_FILES['file']`
  *
  * @return string JSON Response
  *                [[
  *                    $tempUploadedFileName,
  *                    $fileName
  *                ]]
  */
 public function actionUpload()
 {
     $this->actionUpload_clearTempFolder();
     $output_dir = FileUploadMany::$uploadDirectory;
     $info = parse_url($_SERVER['HTTP_REFERER']);
     if ($info['host'] != $_SERVER['SERVER_NAME']) {
         return self::jsonError('Не тот сайт');
     }
     if (isset($_FILES['file'])) {
         $file = $_FILES['file'];
         $ret = [];
         $error = $file['error'];
         //You need to handle  both cases
         //If Any browser does not support serializing of multiple files using FormData()
         if (!is_array($file['name'])) {
             $fileName = \cs\services\UploadFolderDispatcher::generateFileName($file['name']);
             $destPathFull = Yii::getAlias($output_dir);
             FileHelper::createDirectory($destPathFull);
             $destPathFull = $destPathFull . '/' . $fileName;
             move_uploaded_file($file['tmp_name'], $destPathFull);
             $ret[] = [$fileName, $file['name']];
         } else {
             $fileCount = count($file['name']);
             $dir = Yii::getAlias($output_dir);
             for ($i = 0; $i < $fileCount; $i++) {
                 $fileName = $file['name'][$i];
                 move_uploaded_file($file['tmp_name'][$i], $dir . '/' . $fileName);
                 $ret[] = [$fileName, $file['name'][$i]];
             }
         }
         return self::jsonSuccess($ret);
     }
 }
Beispiel #2
0
 /**
  * загружает картинки в CKEDITOR
  */
 public function actionUpload()
 {
     $this->actionUpload_clearTempFolder();
     $fileInfo = pathinfo($_FILES['upload']['name']);
     $path = UploadFolderDispatcher::createFolder(HtmlContent::$uploadFolder);
     $path->add(time() . '_' . Security::generateRandomString(10) . '.' . $fileInfo['extension']);
     if ($_FILES['upload'] == 'none' or empty($_FILES['upload']['name'])) {
         $message = 'No file uploaded';
     } else {
         if ($_FILES['upload']['size'] == 0) {
             $message = 'The file is of zero length';
         } else {
             if ($_FILES['upload']['type'] != 'image/jpeg' and $_FILES['upload']['type'] != 'image/png') {
                 $message = 'The image must be in either JPG or PNG format. Please upload a JPG or PNG instead';
             } else {
                 if (!is_uploaded_file($_FILES['upload']['tmp_name'])) {
                     $message = "You may be attempting to hack our server. We're on to you; expect a knock on the door sometime soon";
                 } else {
                     $message = '';
                     $move = @move_uploaded_file($_FILES['upload']['tmp_name'], $path->getPathFull());
                     if (!$move) {
                         $message = 'Error moving uploaded file. Check the script is granted Read/Write/Modify permissions';
                     }
                 }
             }
         }
     }
     $funcNum = (int) $_GET['CKEditorFuncNum'];
     return Html::script("window.parent.CKEDITOR.tools.callFunction({$funcNum}, '{$path->getPath()}', '{$message}');");
 }
Beispiel #3
0
 public function action()
 {
     $file = UploadedFile::getInstance($this, 'file');
     $path = UploadFolderDispatcher::createFolder('tempFiles');
     $f = explode('.', $file->name);
     if (count($f) == 1) {
         $ext = 'jpg';
     } else {
         $ext = $f[count($f) - 1];
     }
     $path->add(time() . '_' . Security::generateRandomString() . '.' . $ext);
     $file->saveAs($path->getPathFull());
     Yii::$app->session->setFlash('fileName', $path->getPath());
     return true;
 }
Beispiel #4
0
 /**
  * @param \cs\base\BaseForm | \cs\base\DbRecord $model
  * @param array                                 $field
  *
  * @return SitePath
  */
 private static function getContentPath($field, $model)
 {
     $fieldName = $field[BaseForm::POS_DB_NAME];
     return UploadFolderDispatcher::createFolder($model->getTableName(), $model->id, $fieldName);
 }
Beispiel #5
0
 /**
  * Создает папку для загрузки
  *
  * @param array             $field
  * @param \cs\base\BaseForm $model
  *
  * @return \cs\services\SitePath
  */
 protected static function getFolderPath($field, $model)
 {
     $folder = ArrayHelper::getValue($field, 'type.1.folder', $model->getTableName());
     return UploadFolderDispatcher::createFolder('FileUpload2', $folder, $model->id);
 }
Beispiel #6
0
 /**
  * Устанавливает новый аватар
  * Картинка должна быть квадратной
  * Размер 300х300
  *
  * @param string $content   содержимое файла аватара
  * @param string $extension расширение файла
  *
  * @return \cs\services\SitePath
  */
 public function setAvatarAsContent($content, $extension)
 {
     $path = UploadFolderDispatcher::createFolder('FileUpload2', self::TABLE, $this->getId());
     $path->addAndCreate('small');
     $path->add('avatar.' . $extension);
     file_put_contents($path->getPathFull(), $content);
     $this->update(['avatar' => $path->getPath()]);
     return $path;
 }
Beispiel #7
0
 /**
  * @param array             $field
  * @param \cs\base\BaseForm $model
  *
  * @return array поля для обновления в БД
  */
 public static function onUpdate($field, $model)
 {
     $fieldName = $field[BaseForm::POS_DB_NAME];
     $files = $model->{$fieldName};
     $tableName = $model->getTableName();
     $fieldDb = ModelFields::insertOne(['table_name' => $tableName, 'field_name' => $fieldName]);
     ModelFiles::deleteByCondition(['row_id' => $fieldDb->getId()]);
     $folder = \cs\services\UploadFolderDispatcher::createFolder('FileUploadMany', $fieldDb->getId(), $model->id);
     $allFiles = [];
     foreach ($files as $file) {
         $allFiles[] = $file[0];
     }
     $currentFiles = (new Query())->select('w.file_path, w.id')->from('widget_uploader_many w')->innerJoin('widget_uploader_many_fields wf', 'w.field_id = wf.id')->where(['w.row_id' => $model->id, 'wf.table_name' => $model->getTableName(), 'wf.field_name' => $fieldName])->all();
     foreach ($currentFiles as $currentFile) {
         if (!in_array($currentFile['file_path'], $allFiles)) {
             (new SitePath($currentFile['file_path']))->deleteFile();
             $id = (int) $currentFile['id'];
             ModelFiles::deleteByCondition(['id' => $id]);
         }
     }
     foreach ($files as $file) {
         if (StringHelper::startsWith($file[0], '/')) {
             continue;
         }
         $sourcePathFull = Yii::getAlias(self::$uploadDirectory . '/' . $file[0]);
         $destinationFile = $folder->cloneObject($file[0]);
         copy($sourcePathFull, $destinationFile->getPathFull());
         ModelFiles::insert(['file_path' => $destinationFile->getPath(), 'file_name' => $file[1], 'row_id' => $model->id, 'field_id' => $fieldDb->getId(), 'datetime' => gmdate('YmdHis')]);
     }
     return [$fieldName => count($files)];
 }