/** * Gets image list. * @param string $attr owner attribute name. * @return array list or error. */ public function run($attr) { $result = array(); \Yii::$app->response->format = Response::FORMAT_JSON; $model = new File(); $model->setAttributes(['ownerAttribute' => $attr, 'uploadUrl' => $this->uploadUrl, 'uploadPath' => $this->uploadPath], false); $path = $model->getDirPath(); $url = $model->getDirUrl(); if (file_exists($path)) { foreach (FileHelper::findFiles($path, $this->fileOptions) as $file) { $result[] = array('thumb' => $url . basename($file), 'image' => $url . basename($file)); } } return $result; }
/** * Uploads file. * @param string $attr Owner model attribute name. * @return array json response. */ public function run($attr) { \Yii::$app->response->format = Response::FORMAT_JSON; $model = new File(); $model->setAttributes(['ownerAttribute' => $attr, 'uploadUrl' => $this->uploadUrl, 'uploadPath' => $this->uploadPath, 'permissions' => $this->permissions, 'customRules' => $this->customRules, 'file' => UploadedFile::getInstanceByName('file')], false); return $model->validate() ? ['filelink' => $model->getDirUrl() . $model->getFileName(), "filename" => $model->getFileName()] : $this->error($model->firstErrors); }