Yii2:无法通过 FileUploadUI 上传文件
Yii2: can't upload file through FileUploadUI
我正在尝试在 yii2 advanced 中通过 FileUploadUI::widget
上传文件。但我无法实现我的目标。我不知道是什么问题,但是文件没有出现在我要上传到的文件夹中。
这是我的看法
<?= FileUploadUI::widget([
'model' => $model,
'attribute' => 'img',
'url' => ['image-upload', 'id' => $model],
'gallery' => false,
'fieldOptions' => [
'accept' => 'image/*'
],
'clientOptions' => [
'maxFileSize' => 2000000
],
// ...
'clientEvents' => [
'fileuploaddone' => 'function(e, data) {
console.log(e);
console.log(data);
}',
'fileuploadfail' => 'function(e, data) {
console.log(e);
console.log(data);
}',
],
]); ?>
这是控制器,我的控制器进入后端,但我想将文件上传到 frontend/web/img/temp
操作
public function actionImageUpload()
{
$model = new MyNews();
$imageFile = UploadedFile::getInstance($model, 'img');
$directory = Yii::getAlias('/../frontend/web/img/temp/');
if ($imageFile != null) { //can't saveAs() file into my ../img/temp/ folder
$uid = 'qqqq';
$fileName = $uid . '.' . $imageFile->extension;
$filePath = $directory . $fileName;
if ($imageFile->saveAs($filePath)) {
$path = '/../frontend/web/img/temp/' . $fileName;
//.....
}
}
return '';
}
你应该使用 Yii 定义的 aliases
,比如 @frontend
@backend
,而不是在 Yii::getAlias()
中提供路径,我所说的一些常见的定义在 common/config/bootstrap.php
喜欢
Yii::setAlias ( '@frontend' , dirname ( dirname ( __DIR__ ) ) . '/frontend' );
Yii::setAlias ( '@backend' , dirname ( dirname ( __DIR__ ) ) . '/backend' );
您提供的路径也是错误的,因为 Yii 在 backend/web/
中,而不是您尝试访问的 backend
应用程序的根目录上传。
我建议在 common/config/bootstrap.php
中定义路径,如下所示
Yii::setAlias('@uploadDir',realpath(Yii::getAlias('@frontend').'/web/img/temp/'));
然后将您的代码更改为以下
public function actionImageUpload()
{
$model = new MyNews();
$imageFile = UploadedFile::getInstance($model, 'img');
$directory = Yii::getAlias('@uploadDir');
if ($imageFile != null) { //can't saveAs() file into my ../img/temp/ folder
$uid = 'qqqq';
$fileName = $uid . '.' . $imageFile->extension;
$filePath = $directory . $fileName;
if ($imageFile->saveAs($filePath)) {
Yii::$app->session->setFlash('success','The file has been uploaded successfuly');
return $this->redirect('index');
}
}
}
我正在尝试在 yii2 advanced 中通过 FileUploadUI::widget
上传文件。但我无法实现我的目标。我不知道是什么问题,但是文件没有出现在我要上传到的文件夹中。
这是我的看法
<?= FileUploadUI::widget([
'model' => $model,
'attribute' => 'img',
'url' => ['image-upload', 'id' => $model],
'gallery' => false,
'fieldOptions' => [
'accept' => 'image/*'
],
'clientOptions' => [
'maxFileSize' => 2000000
],
// ...
'clientEvents' => [
'fileuploaddone' => 'function(e, data) {
console.log(e);
console.log(data);
}',
'fileuploadfail' => 'function(e, data) {
console.log(e);
console.log(data);
}',
],
]); ?>
这是控制器,我的控制器进入后端,但我想将文件上传到 frontend/web/img/temp
操作
public function actionImageUpload()
{
$model = new MyNews();
$imageFile = UploadedFile::getInstance($model, 'img');
$directory = Yii::getAlias('/../frontend/web/img/temp/');
if ($imageFile != null) { //can't saveAs() file into my ../img/temp/ folder
$uid = 'qqqq';
$fileName = $uid . '.' . $imageFile->extension;
$filePath = $directory . $fileName;
if ($imageFile->saveAs($filePath)) {
$path = '/../frontend/web/img/temp/' . $fileName;
//.....
}
}
return '';
}
你应该使用 Yii 定义的 aliases
,比如 @frontend
@backend
,而不是在 Yii::getAlias()
中提供路径,我所说的一些常见的定义在 common/config/bootstrap.php
喜欢
Yii::setAlias ( '@frontend' , dirname ( dirname ( __DIR__ ) ) . '/frontend' );
Yii::setAlias ( '@backend' , dirname ( dirname ( __DIR__ ) ) . '/backend' );
您提供的路径也是错误的,因为 Yii 在 backend/web/
中,而不是您尝试访问的 backend
应用程序的根目录上传。
我建议在 common/config/bootstrap.php
中定义路径,如下所示
Yii::setAlias('@uploadDir',realpath(Yii::getAlias('@frontend').'/web/img/temp/'));
然后将您的代码更改为以下
public function actionImageUpload()
{
$model = new MyNews();
$imageFile = UploadedFile::getInstance($model, 'img');
$directory = Yii::getAlias('@uploadDir');
if ($imageFile != null) { //can't saveAs() file into my ../img/temp/ folder
$uid = 'qqqq';
$fileName = $uid . '.' . $imageFile->extension;
$filePath = $directory . $fileName;
if ($imageFile->saveAs($filePath)) {
Yii::$app->session->setFlash('success','The file has been uploaded successfuly');
return $this->redirect('index');
}
}
}