Rumah >rangka kerja php >YII >yii框架实现图片上传源码分享

yii框架实现图片上传源码分享

王林
王林ke hadapan
2020-12-18 09:43:532143semak imbas

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框架

Atas ialah kandungan terperinci yii框架实现图片上传源码分享. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Artikel ini dikembalikan pada:csdn.net. Jika ada pelanggaran, sila hubungi admin@php.cn Padam
Artikel sebelumnya:yii怎么手动生成错误日志Artikel seterusnya:yii框架项目部署步骤