Yii 2.0 文件上传

先创建一个(UploadForm.php)模型层

<?php
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 file attribute
    */
    public $file;

/**
    * @return array the validation rules.
    */
    public function rules()
    {
        //‘skipOnEmpty‘ => false  验证
        //‘extensions‘ => ‘png, jpg, gif, jpeg‘  上传的文件的格式
        return [
            [[‘file‘], ‘file‘],
        ];
    }
}

------------------------------------------------------------------------------

控制器层 (UploadController.php)

<?php
namespace backend\controllers;

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

class UploadController extends Controller{
    function actionIndex(){
        $request = Yii::$app->request;
        $model = new UploadForm();
        if($request->isPost)
        {
            $model->file = UploadedFile::getInstance($model, ‘file‘);

if ($model->file && $model->validate())
            {
                $r = $model->file->saveAs(‘./upload/‘ . $model->file->baseName . ‘.‘. $model->file->extension);    //这是上传,upload文件夹要自己手动创建
                //获取上传文件的名称
                $r = $model->file;
                $image= $r->name;
                
                //添加入库
                $upload = \Yii::$app->db;
                $query = $upload->createCommand()->insert("upload",[‘image‘=>$image]);
                if($query->execute())
                {
                    $this->redirect(array(‘upload/index‘));
                }
            }
        }
        else
        {
            return $this->render(‘upload‘,[‘model‘ => $model]);
        }
    }
}

-----------------------------------------------------------------------------------------------------

视图层(upload.php)

<?php
use yii\widgets\ActiveForm;
use yii\helpers\Url;
?>

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

<table class="table table-bordered table-hover definewidth m10">
<tr>
   <td class="tableleft">上传图片</td>
   <td><?= $form->field($model, ‘file‘)->fileInput() ?></td>
</tr>

<tr>
   <td class="tableleft"></td>
   <td>
       <button type="submit" class="btn btn-primary" type="button">保存</button>
   </td>
</tr>

<?php ActiveForm::end() ?>

---恢复内容结束---

---恢复内容开始---

先创建一个(UploadForm.php)模型层

<?php
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 file attribute
    */
    public $file;

/**
    * @return array the validation rules.
    */
    public function rules()
    {
        //‘skipOnEmpty‘ => false  验证
        //‘extensions‘ => ‘png, jpg, gif, jpeg‘  上传的文件的格式
        return [
            [[‘file‘], ‘file‘],
        ];
    }
}

------------------------------------------------------------------------------

控制器层 (UploadController.php)

<?php
namespace backend\controllers;

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

class UploadController extends Controller{
    function actionIndex(){
        $request = Yii::$app->request;
        $model = new UploadForm();
        if($request->isPost)
        {
            $model->file = UploadedFile::getInstance($model, ‘file‘);

if ($model->file && $model->validate())
            {
                $r = $model->file->saveAs(‘./upload/‘ . $model->file->baseName . ‘.‘. $model->file->extension);    //这是上传,upload文件夹要自己手动创建
                //获取上传文件的名称
                $r = $model->file;
                $image= $r->name;
                
                //添加入库
                $upload = \Yii::$app->db;
                $query = $upload->createCommand()->insert("upload",[‘image‘=>$image]);
                if($query->execute())
                {
                    $this->redirect(array(‘upload/index‘));
                }
            }
        }
        else
        {
            return $this->render(‘upload‘,[‘model‘ => $model]);
        }
    }
}

-----------------------------------------------------------------------------------------------------

视图层(upload.php)

<?php
use yii\widgets\ActiveForm;
use yii\helpers\Url;
?>

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

<table class="table table-bordered table-hover definewidth m10">
<tr>
   <td class="tableleft">上传图片</td>
   <td><?= $form->field($model, ‘file‘)->fileInput() ?></td>
</tr>

<tr>
   <td class="tableleft"></td>
   <td>
       <button type="submit" class="btn btn-primary" type="button">保存</button>
   </td>
</tr>

<?php ActiveForm::end() ?>

时间: 2024-12-14 05:21:18

Yii 2.0 文件上传的相关文章

servlet3.0文件上传功能

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); //获取文件部件part Part part = request.getPart("file"); String h = part.getHeader(&

jsp Servlet 3.0文件上传

Servlet 3.0之前上传文件一般都要借助与第三方插件上传,有了servlet3.0后,上传文件从此变得简单.老规矩,直接上代码. 1.建立一个index.jsp用于表单提交 1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3

java servlet 3.0文件上传

在以前,处理文件上传是一个很痛苦的事情,大都借助于开源的上传组件,诸如commons fileupload等.现在好了,很方便,便捷到比那些组件都方便至极.以前的HTML端上传表单不用改变什么,还是一样的multipart/form-data MIME类型. 让Servlet支持上传,需要做两件事情 需要添加MultipartConfig注解 从request对象中获取Part文件对象 但在具体实践中,还是有一些细节处理,诸如设置上传文件的最大值,上传文件的保存路径. 需要熟悉Multipart

servlet3.0文件上传

1.浏览器端:选择图片.提交表单,同时将图片发送给服务器<form action="" method="post" enctype="multipart/form-data"><input type="file" name="image"><input type="submit">上传的内容,都在请求体中.2.服务器端:1)手动获得请求体,需要手动解析

yii2.0 文件上传

Yii 2.0 出来好长时间了,一直都是看下官方网站,没实践过,今天弄了下图片上传操作. 1创建一个简单的数据表 mysql> desc article; +---------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------+--------------+------+-----+---------+--------

laravel oauth2.0 文件上传报错

报错信息:   "message": "Invalid stream or file provided for UploadedFile",    "exception": "InvalidArgumentException",    "file": "E:\\unionnet\\vendor\\zendframework\\zend-diactoros\\src\\UploadedFile.ph

用VSCode开发一个asp.net core2.0+angular5项目(5): Angular5+asp.net core 2.0 web api文件上传

第一部分: http://www.cnblogs.com/cgzl/p/8478993.html 第二部分: http://www.cnblogs.com/cgzl/p/8481825.html 第三部分: https://www.cnblogs.com/cgzl/p/8525541.html 第四部分: https://www.cnblogs.com/cgzl/p/8536350.html 这部分就讲从angular5的客户端上传图片到asp.net core 2.0的 web api. 这是

Servlet3.0简化实现文件上传

注意事项 POST请求可以传输二进制数据,而GET只能传输文本信息 form表单标签中声明属性 enctype="multipart/form-data" 给Servlet添加注解@MultipartConfig,开启Servlet3.0文件上传组件开关 Servlet实现 @WebServlet(name = "UploadServlet",value = "/upload") @MultipartConfig public class Upl

Java中实现文件上传下载的三种解决方案

第一点:Java代码实现文件上传 FormFile file=manform.getFile(); String newfileName = null; String newpathname=null; String fileAddre="/numUp"; try { InputStream stream = file.getInputStream();// 把文件读入 String filePath = request.getRealPath(fileAddre);//取系统当前路径