如何将文件上传到yii2中的目录? [英] How to upload a file to directory in yii2?

查看:447
本文介绍了如何将文件上传到yii2中的目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ActiveForm,我想添加一个用户可以在其中上传照片的字段. 问题是我没有用户表中的图像属性,并且每个 "yii"中的输入字段需要一个模型和一个属性,如下所示.

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.

我还检查了kartik写的库,但还需要一个属性字段.

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

推荐答案

遵循官方文档

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

表单模型

namespace app\models;

use yii\base\Model;
use yii\web\UploadedFile;

/**
* 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'],
    ];
}
}
?>

表单视图

<?php
use yii\widgets\ActiveForm;

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

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

<button>Submit</button>

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

控制器

现在创建将表单和模型连接在一起的控制器:

Now create the controller that connects form and model together:

<?php
namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\models\UploadForm;
use yii\web\UploadedFile;

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]);
}
}
?>

我们使用的是UploadedFile::getInstance(...)而不是model->load(...). [[\yii\web\UploadedFile|UploadedFile]]不运行模型验证.它仅提供有关上载文件的信息.因此,您需要通过$model->validate()手动运行验证.这会触发需要文件的[[yii\validators\FileValidator|FileValidator]]:

Instead of model->load(...) we are using UploadedFile::getInstance(...). [[\yii\web\UploadedFile|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 [[yii\validators\FileValidator|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.

就是这样.加载页面并尝试上传.升级应该以basic/web/uploads结尾.

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

这篇关于如何将文件上传到yii2中的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆