首页 > php框架 > YII > 正文

yii实现图片上传

王林
发布: 2020-11-27 16:54:51
转载
9198 人浏览过

yii实现图片上传

具体代码如下:

(推荐教程:yii

1、model

<?php
namespace frontend\models;
 
use yii\base\Model;
use yii\web\UploadedFile;
use yii\db\ActiveRecord;
use yii\db\Query;
 
class UploadForm extends ActiveRecord
{
    /**
     * @var UploadedFile
     */
    public $t_img;
    public $t_title;
    public $t_content;
    public function rules()
    {
        return [
            [[&#39;t_img&#39;], &#39;file&#39;, &#39;skipOnEmpty&#39; => false, &#39;extensions&#39; => &#39;png, jpg,bmp,jpeg&#39;],
        ];
    }
    public function attributeLabels()
    {
        return [
            &#39;t_img&#39;=>&#39;请上传文章图片&#39;,
            &#39;verifyCode&#39; => &#39;请在右面输入验证码&#39;,
        ];
    }
 
 
    public function upload()
    {
        $imgName=time().rand(100,999).".".$this->t_img->extension;
        if ($this->validate()) {
            $this->t_img->saveAs(&#39;uploads/&#39; .$imgName);
            $path=&#39;uploads/&#39; .$imgName;
            return $path;
        } else {
            return false;
        }
    }
}
 
?>
登录后复制

2、控制器

 $data=Yii::$app->request->post();
            $data[&#39;t_addtime&#39;]=date(&#39;Y-m-d H:i:s&#39;);
            $upload->t_img = UploadedFile::getInstance($upload, &#39;t_img&#39;);
            $path=$upload->upload();
登录后复制

3、视图层

<?php
use yii\widgets\ActiveForm;
use yii\helpers\Html;
use yii\helpers\Url;
?>
<?=Html::a(&#39;返回&#39;,&#39;?r=course/classspace&c_id=&#39;.$c_id)?>
<?php $form=ActiveForm::begin(
    [
        &#39;options&#39; => [&#39;enctype&#39; => &#39;multipart/form-data&#39;],
        &#39;method&#39;=>&#39;POST&#39;,
    ]
);?>
<table class="table">
 
    <tr>
        <td>
 
            <input type="text" placeholder="请填写话题标题" name="t_title" id="t_title" value=<?=$coursedraft[&#39;d_title&#39;]?>   >
        </td>
        </tr>
    <tr>
        <td>
            <textarea name="t_content" id="t_content" cols="30" rows="10" placeholder="请填写话题内容"><?=$coursedraft[&#39;d_content&#39;]?></textarea>
        </td>
    </tr>
    <tr>
        <td>
           <?=$form->field($upload,&#39;t_img&#39;)->fileInput()?>
        </td>
    </tr>
    <tr>
        <div class="btn-group">
            <td>
                <?=Html::submitButton(&#39;提交话题&#39;,[&#39;class&#39;=>&#39;btn btn-success&#39;])?>
            </td>
        </div>
 
    </tr>
</table>
<?php ActiveForm::end();?>
<input type="hidden" value=<?=$c_id?>  id="c_id" />
</body>
<?php
$js = <<<END
    $(function(){
//        $(document).on(&#39;click&#39;,&#39;#caogao&#39;,function() {
//            var title = $("#t_title").val();
//            var content = $("#t_content").val();
//
//            $.ajax({
//                type: "POST",
//                url: "?r=course/coursedraft",
//                data: {t_title: title, t_content: content, d_id: d_id}
//            })
//        })
        function show(){
            var title=$("#t_title").val();
            var content=$("#t_content").val();
            var c_id=$(&#39;#c_id&#39;).val();
            $.ajax({
                type: "POST",
                url: "?r=course/coursedraft",
                data: {d_title:title,d_content:content,c_id:c_id,d_state:0}
            });
        }
        setInterval(show,5000);
    })
END;
$this->registerJs($js);
?>
</html>
登录后复制

以上是yii实现图片上传的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:csdn.net
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板