Home > PHP Framework > YII > body text

yii implements image uploading

王林
Release: 2020-11-27 16:54:51
forward
9190 people have browsed it

yii implements image uploading

The specific code is as follows:

(Recommended tutorial: 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;
        }
    }
}
 
?>
Copy after login

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();
Copy after login

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>
Copy after login

The above is the detailed content of yii implements image uploading. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template