bambinou
(Benoitrivaux)
April 27, 2015, 10:17pm
1
Hello,
I have found this tutorial about uploading a single file which is what I am trying to do:
Uploading Files
===============
Uploading files in Yii is usually done with the help of [[yii\web\UploadedFile]] which encapsulates each uploaded
file as an `UploadedFile` object. Combined with [[yii\widgets\ActiveForm]] and [models](structure-models.md),
you can easily implement a secure file uploading mechanism.
## Creating Models <span id="creating-models"></span>
Like working with plain text inputs, to upload a single file you would create a model class and use an attribute
of the model to keep the uploaded file instance. You should also declare a validation rule to validate the file upload.
For example,
```php
namespace app\models;
use yii\base\Model;
use yii\web\UploadedFile;
This file has been truncated. show original
There is one thing I do not yet understand, they say to create a model and controller but what if this upload field is part of another form which runs with another controller and model?
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
<?= $form->field($model, 'file')->fileInput() ?>
Let’s say I have this form:
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'title')->textInput(['maxlength' => 160]) ?>
<?= $form->field($model, 'posts')->textarea(['rows' => 6]) ?>
<?= $form->field($model, 'keywords')->textInput(['maxlength' => 45]) ?>
<?= $form->field($model, 'categories_id')->dropDownList(
ArrayHelper::map(Categories::find()->all(),'id','name'),
['prompt'=>'Select a category']) ?>
<?= $form->field($model, 'user_id')->textInput(['value' => \Yii::$app->user->identity->id]) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
How to put this upload data into it?
Finally, what I do not understand is how to retrieve the file path and where will the file be recorded in the database if I wish to output the picture in another view?
Thank you,
Ben
The Link You have is Too enough for file Uploading, simply use that one, for other models use that model name , and use getinstance of that model from the controller, save image name in db, and file in filepath (eg./images/yourimagename.ext),
for view , use basePath and image tags.
bambinou
(Benoitrivaux)
April 28, 2015, 10:06am
3
Thank you for the reply.
Do you know a good tutorial somewhere please as I am still new to Yii2.
I cannot find anything that shows a step by step on the Yii website.
Thank you,
Ben
AlexInt
(Alex)
April 28, 2015, 10:27am
4
In your form you just have to add a file input and ‘enctype’ => ‘multipart/form-data’ to the form widget options.
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
//with model
<?= $form->field($model, 'file')->fileInput() ?>
//without model
<?= yii\helpers\Html::fileInput('file-name'); ?>
Now on your controller you take the file like this:
if (Yii::$app->request->isPost) {
//with model
$model->file = UploadedFile::getInstance($model, 'file');
$model->file->saveAs('uploads/' . $model->file->baseName . '.' . $model->file->extension)
//without model
$file = UploadedFile::getInstanceByName('file-name');
$file->saveAs('uploads/' . $file->baseName . '.' . $file->extension)
}
bambinou
(Benoitrivaux)
April 28, 2015, 5:07pm
5
And that’s all I need really?
No name space at the top or helpers or anything similar?
When you said in the the controller, do you mean in the actionCreate()?
public function actionCreate()
{
$model = new Posts();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
Thank you,
Ben
AlexInt
(Alex)
April 30, 2015, 8:31am
6
That’s all you need.
Of course, you will need all the namespaces, I didn’t mention that because is obvious.
And yes, when I said in your controller I meant in your actionCreate() or whatever is called.
bambinou
(Benoitrivaux)
April 30, 2015, 10:05pm
7
Thanks Alexint,
I have a similar code to yours but keep getting an error as stated on this thread, any idea what is wrong please?
http://www.yiiframework.com/forum/index.php/topic/63524-class-frontendcontrollersuploadedfile-not-found/page__gopid__279946#entry279946
Thanks,
Ben