当前位置:首页 > 编程教程 > php技术文章 > yii2 上传图片的示例代码

yii2上传图片的代码讲解

  • 发布时间:
  • 作者:码农之家
  • 点击:121

这篇文章主要知识点是关于yii2、上传图片、的内容,如果大家想对相关知识点有系统深入的学习,可以参阅以下电子书

Learning PHP设计模式
Learning PHP设计模式高质量中文版
  • 类型:PHP编程大小:39.4 MB格式:PDF作者:William Sanders
立即下载

yii2 上传图片的示例代码

第一步: 搭建上传类基础工作

第二步:建站一个product表,字段id,name,picurl.

第三步:生GII生成PRODUCT 模型,类,视图。

第四步:

main.css 放在frontend\web\css

.onedialog{position:absolute; left: 300px; top: 500px; z-index: 10; width: 700px; height: 400px;border-radius:5px;box-shadow:5px 2px 6px #000; border: 2px solid #666}
.oneiframe{ width: 100%; height: 100% }

main.js 放在 frontend\web\assets

$(function(){
 $('#product-picurl').click(function(){
 $('#oneupload').remove();
 $('<div>').appendTo($('body')).attr({"class":"onedialog",'id':"oneupload"});
 $('<iframe>').appendTo($('#oneupload')).attr({"src":"?r=upload","class":"oneiframe"})
 });
  var v=$('#product-picurl').val();
 if(v){
 $('<img>').attr({"src":v,"style":"height:50px"}).insertAfter($('#product-picurl'));
 }
});

然后在frontend\assets\AppAsset.php中注册这两个文件

class AppAsset extends AssetBundle
{
  public $basePath = '@webroot';
  public $baseUrl = '@web';
  public $css = [
    'css/site.css',
    'css/main.css',
  ];
  public $js = [
    'assets/main.js'
  ];
  public $depends = [
    'yii\web\YiiAsset',
    'yii\bootstrap\BootstrapAsset',
  ];
}

UploadController.php

<?PHP
namespace frontend\controllers;

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

class UploadController extends Controller
{
  public function actionIndex()
  {
    $model = new UploadForm();

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

      if ($model->file && $model->validate()) {
        //$model->file->saveAs('uploads/' . $model->file->baseName . '.' . $model->file->extension);      
        $fileName='uploads/' . date("YmdHis") . '.' . $model->file->extension;
        $model->file->saveAs($fileName);
      }
      echo "<script src='assets/upload.js'></script>;";
      echo "<script>";
      echo "var oneinput=parent.document.getElementById('product-picurl');";
      echo "parent.document.getElementById('product-picurl').value='".$fileName."';";
      echo "var oneupload = parent.document.getElementById('oneupload');";
      echo "var img = document.createElement('img');";
      echo "img.setAttribute('style', 'height:50px');";
      echo "img.src ='".$fileName."';";
      echo "insertAfter(img,oneinput);";
      echo "oneupload.parentNode.removeChild(oneupload)";
      echo "</script>";
    }

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

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()
  {
    return [
      [['file'], 'file'],
    ];
  }
}
?>

upload.php

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

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

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

<button>Submit</button>

<?php ActiveForm::end() ?>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持码农之家。

以上就是本次给大家分享的关于java的全部知识点内容总结,大家还可以在下方相关文章里找到相关文章进一步学习,感谢大家的阅读和支持。

您可能感兴趣的文章:

  • 解析yii2页面底部加载css和js的方法
  • Yii2.0乐观锁与悲观锁的原理用法
  • Yii2 rbac权限控制操作方法及实例解析
  • 实例分享Yii2中SqlDataProvider用法
  • 关于Yii2 定制表单输入字段的标签和样式
  • Yii2实现多域名跨域同步登录退出的实例代码及讲解
  • Yii2.0 rules验证规则总结分享
  • yii2中LinkPager如何增加总页数和总记录数的示例代码
  • yii2中关于加密解密的知识点总结
  • 实例分享Yii2主题(Theme)用法
  • 相关电子书
    学习笔记
    网友NO.584263

    Yii2实现ajax上传图片插件用法

    本文实例讲述了Yii2实现ajax上传图片插件用法。分享给大家供大家参考,具体如下: 这里需要先下载 mdmsoft/yii2-upload-file.插件。 代码如下: public function actionCreate(){ $model = new DynamicModel([ 'nama', 'file_id' ]); // behavior untuk upload file $model-attachBehavior('upload', [ 'class' = 'mdm\upload\UploadBehavior', 'attribute' = 'file', 'savedAttribute' = 'file_id' // coresponding with $model-file_id ]); // rule untuk model $model-addRule('nama', 'string') -addRule('file', 'file', ['extensions' = 'jpg']); if ($model-load(Yii::$app-request-post()) $model-validate()) { if ($model-saveUploadedFile() !== false) { Yii::$app-session-setFlash('success', 'Upload Sukses'); } } return $this-render('upload',['model' = $model]);} Then in view upload.php ?phpuse yii\helpers\Html;use yii\widgets\ActiveForm;/* @var $this yii\web\View */?div ?php $form = ActiveForm::begin([ 'options' = [ 'enctype' = 'multipart/form-data'] ]); ? ?= $form-field($model,……

    网友NO.360664

    Yii2.0多文件上传实例说明

    创建控制器FormController ?php namespace frontend\controllers;use Yii;use yii\web\Controller;use frontend\models\Uploadm;use yii\web\UploadedFile;class FormController extends Controller{ public function actionMyfiles(){ $model=new Uploadm(); return $this-renderPartial('myfiles',['model'=$model]); } public function actionGetfiles(){ $model = new Uploadm(); if (Yii::$app-request-isPost) { $model-imgFile = UploadedFile::getInstances($model, 'imgFile'); if ($model-upload()) { // 文件上传成功 echo '上传成功'; } } } 创建模型Uploadm.php ?phpnamespace frontend\models;use Yii;use yii\base\Model;use yii\web\UploadedFile;class Uploadm extends Model{ public $imgFile; public function rules(){ return [ [['imgFile'], 'file','maxFiles' = 5],//最多5张 ]; } public function upload() { if ($this-validate()) { foreach ($this-imgFile as $file) { $file-saveAs('uploads/' . $file-baseName . '.' . $file-extension); } return true; } else { return false; } }} 创建视图/views/form/myfiles.p……

    <
    1
    >

    Copyright 2018-2020 www.xz577.com 码农之家

    投诉 / 推广 / 赞助:QQ:520161757