如何在yii2高级模板中上传根文件夹中的文件? [英] How to upload files in root folder in yii2 advanced template?

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

问题描述

我无法上传根文件夹中的文件.

I am unable to upload files in root folder.

我将文件上传到根文件夹并在前端和后端访问这些文件申请.

I uploaded files in root folder and access these files in frontend and backend application.

推荐答案

使用高级模板时,您需要决定一个公共位置来存储前端和后端应用程序都可以访问的文件,另外,如果您愿意要通过网络公开访问的文件,您需要确保该位置是公共文件夹.

When using the advanced template you need to decide on a common place to store your files that can be accessed by both the frontend and backend application, additionally if you want the files to be publicly accessible via the web you need to insure this location is a public folder.

我倾向于使用 frontend/web 文件夹作为我常用的上传位置.从后端上传时,我会写入此位置.然后我可以在前端使用这些图像.

I tend to use the frontend/web folder as my common upload location. When uploading from the backend I write to this location. I can then use the images in the frontend.

示例从后端上传.

UploadForm.php

创建一个模型来管理上传数据,确保包含文件属性.

Create a model to manage the upload data, make sure to include the file attribute.

class UploadForm extends Model
{
    /**
     * @var UploadedFile file attribute
     */
    public $file;

    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            [['file'], 'file', 'extensions'=>'jpg, gif, png'],
        ];
    }
}

上传控制器

在将管理上传的控制器中,使用前端的别名来设置上传路径 $path = Yii::getAlias('@frontend') .'/web/uploads/'

In the controller that will be managing the upload use the alias for the frontend to set your upload path $path = Yii::getAlias('@frontend') .'/web/uploads/'

class MediaController extends Controller
{

    public function actionIndex()
    {

        $model = new UploadForm();

        //Set the path that the file will be uploaded to
        $path = Yii::getAlias('@frontend') .'/web/upload/'

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

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

        return $this->renderPartial('index', ['model' => $model]);

    }
}

查看表单

向视图添加表单,确保设置 'multipart/form-data' enctype 以便它可以接受文件上传.

Add a form to your view, make sure to set the 'multipart/form-data' enctype so that it can accept file uploads.

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

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

    <?= Html::submitButton('Upload') ?>

<?php ActiveForm::end() ?>

前端

然后您可以通过/upload/{image-name}.{extension} 访问前端中的图像.示例

You can then access the image in the front end via /upload/{image-name}.{extension}. example <img src="/upload/sample.png">

注意:最好将上传路径存储在 common/config/params.php 中,以便您可以从前端和后端访问.

Note: it is a good idea to store your upload path in the common/config/params.php so that you can access from both frontend and backend.

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

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