Home  >  Article  >  PHP Framework  >  Yii framework implements image upload source code sharing

Yii framework implements image upload source code sharing

王林
王林forward
2020-12-18 09:43:532043browse

Yii framework implements image upload source code sharing

The following is the source code for the yii framework to implement the image upload function. I hope it can be helpful to everyone.

(Learning video sharing: Programming video)

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, controller

 $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. View layer

<?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>

Related recommendations: yii framework

The above is the detailed content of Yii framework implements image upload source code sharing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete