hi,你好!欢迎访问本站!登录
本站由简数采集腾讯云宝塔系统阿里云强势驱动
当前位置:首页 - PHP框架 - YII - 正文 请牢记本站网址www.sosophp.cn

yii2 怎样上传图片【yii框架教程】,yii2,上传图片

2019-12-08YII搜搜PHP网48°c
A+ A-

第一步:搭建上传类基本事情,细致请看:http://www.yiichina.com/tutorial/328

第二步:建站一个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() ?>

ki4网,有大批免费的Yii入门教程,迎接人人进修!

以上就是yii2 怎样上传图片的细致内容,更多请关注ki4网别的相干文章!

  选择打赏方式
微信赞助

打赏

QQ钱包

打赏

支付宝赞助

打赏

  选择分享方式
  移步手机端
yii2 怎样上传图片【yii框架教程】,yii2,上传图片

1、打开你手机的二维码扫描APP
2、扫描左则的二维码
3、点击扫描获得的网址
4、可以在手机端阅读此文章
标签: