这篇文章主要介绍了yii2 上传图片的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
第一步: 搭建上传类基础工作,具体请看://www.mobange.com/article/120242.htm
第二步:建站一个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="YXNzZXRzL3VwbG9hZC5qcw=="></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 ="Ii4kZmlsZU5hbWUu"';";
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() ?>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
沃梦达教程
本文标题为:yii2 上传图片的示例代码
猜你喜欢
- 用nohup命令实现PHP的多进程 2023-09-02
- windows下9款一键快速搭建PHP本地运行环境的好工具(含php7.0环境) 2023-09-02
- Laravel balde模板文件中判断数据为空方法 2023-08-30
- PHP中PDO事务处理操作示例 2022-10-15
- laravel通用化的CURD的实现 2023-03-17
- PHP仿tp实现mvc框架基本设计思路与实现方法分析 2022-10-18
- PHP简单实现二维数组的矩阵转置操作示例 2022-10-02
- PHP实现微信支付(jsapi支付)流程步骤详解 2022-10-09
- laravel实现按月或天或小时统计mysql数据的方法 2023-02-22
- php微信公众号开发之秒杀 2022-11-23