這篇文章將為大家詳細(xì)講解有關(guān)yii框架如何實現(xiàn)圖片上傳功能,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

成都創(chuàng)新互聯(lián)主營淄博網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都APP應(yīng)用開發(fā),淄博h5小程序設(shè)計搭建,淄博網(wǎng)站營銷推廣歡迎淄博等地區(qū)企業(yè)咨詢
yii框架實現(xiàn)圖片上傳功能的源碼:
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 [
[['t_img'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg,bmp,jpeg'],
];
}
public function attributeLabels()
{
return [
't_img'=>'請上傳文章圖片',
'verifyCode' => '請在右面輸入驗證碼',
];
}
public function upload()
{
$imgName=time().rand(100,999).".".$this->t_img->extension;
if ($this->validate()) {
$this->t_img->saveAs('uploads/' .$imgName);
$path='uploads/' .$imgName;
return $path;
} else {
return false;
}
}
}
?>2、控制器
$data=Yii::$app->request->post();
$data['t_addtime']=date('Y-m-d H:i:s');
$upload->t_img = UploadedFile::getInstance($upload, 't_img');
$path=$upload->upload();3、視圖層
<?php
use yii\widgets\ActiveForm;
use yii\helpers\Html;
use yii\helpers\Url;
?>
<?=Html::a('返回','?r=course/classspace&c_id='.$c_id)?>
<?php $form=ActiveForm::begin(
[
'options' => ['enctype' => 'multipart/form-data'],
'method'=>'POST',
]
);?>
<table class="table">
<tr>
<td>
<input type="text" placeholder="請?zhí)顚懺掝}標(biāo)題" name="t_title" id="t_title" value=<?=$coursedraft['d_title']?> >
</td>
</tr>
<tr>
<td>
<textarea name="t_content" id="t_content" cols="30" rows="10" placeholder="請?zhí)顚懺掝}內(nèi)容"><?=$coursedraft['d_content']?></textarea>
</td>
</tr>
<tr>
<td>
<?=$form->field($upload,'t_img')->fileInput()?>
</td>
</tr>
<tr>
<div class="btn-group">
<td>
<?=Html::submitButton('提交話題',['class'=>'btn btn-success'])?>
</td>
</div>
</tr>
</table>
<?php ActiveForm::end();?>
<input type="hidden" value=<?=$c_id?> id="c_id" />
</body>
<?php
$js = <<<END
$(function(){
// $(document).on('click','#caogao',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=$('#c_id').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>關(guān)于yii框架如何實現(xiàn)圖片上傳功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
分享標(biāo)題:?yii框架如何實現(xiàn)圖片上傳功能
鏈接分享:http://www.chinadenli.net/article30/isjgso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)站策劃、服務(wù)器托管、微信小程序、用戶體驗、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)