I'm trying to upload a file through FileUploadUI::widget
in yii2 advanced. But I can't achieve my goal. I don't know what the problem is, but the file doesn't appear in the folder I wish to upload to.
this is my view
<?= 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);
}',
],
]); ?>
this is controller my controller is into backend but I want to upload the file to frontend/web/img/temp
action
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 '';
}