Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
427 views
in Technique[技术] by (71.8m points)

php - How to upload a file to directory in yii2?

i have an ActiveForm, and i want to add a field where the user can upload their photos. the problem is that i don't have an attribute for the image in the users table and every input field in 'yii' expects a model and an attribute as follows.

<?= $form->field($model, 'attribute')->input($platforms) ?>

i don't want to assign the image to any record nor i want to insert in in the database, i want it to be uploaded to a specific folder.

i have also checked the library kartik wrote, but also requires an attribute field.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Follow the official documentation

https://github.com/yiisoft/yii2/blob/master/docs/guide/input-file-upload.md

Form Model

namespace appmodels;

use yiiaseModel;
use yiiwebUploadedFile;

/**
* UploadForm is the model behind the upload form.
*/
class UploadForm extends Model
{
/**
 * @var UploadedFile|Null file attribute
 */
public $file;

/**
 * @return array the validation rules.
 */
public function rules()
{
    return [
        [['file'], 'file'],
    ];
}
}
?>

Form View

<?php
use yiiwidgetsActiveForm;

$form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>

<?= $form->field($model, 'file')->fileInput() ?>

<button>Submit</button>

<?php ActiveForm::end(); ?>

Controller

Now create the controller that connects form and model together:

<?php
namespace appcontrollers;

use Yii;
use yiiwebController;
use appmodelsUploadForm;
use yiiwebUploadedFile;

class SiteController extends Controller
{
public function actionUpload()
{
    $model = new UploadForm();

    if (Yii::$app->request->isPost) {
        $model->file = UploadedFile::getInstance($model, 'file');

        if ($model->validate()) {                
            $model->file->saveAs('uploads/' . $model->file->baseName . '.' . $model->file->extension);
        }
    }

    return $this->render('upload', ['model' => $model]);
}
}
?>

Instead of model->load(...) we are using UploadedFile::getInstance(...). [[yiiwebUploadedFile|UploadedFile]] does not run the model validation. It only provides information about the uploaded file. Therefore, you need to run validation manually via $model->validate(). This triggers the [[yiivalidatorsFileValidator|FileValidator]] that expects a file:

 $file instanceof UploadedFile || $file->error == UPLOAD_ERR_NO_FILE //in code framework

If validation is successful, then we're saving the file:

 $model->file->saveAs('uploads/' . $model->file->baseName . '.' . $model->file->extension);

If you're using "basic" application template then folder uploads should be created under web.

That's it. Load the page and try uploading. Uplaods should end up in basic/web/uploads.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...