
批改狀態(tài):合格
老師批語:
1、修改public/.htaccess文件配置,在index.php后加?,如下:
RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L]
2、引入layui.js文件:
<script src="/static/layui/layui.js"></script>
3、引入layui的layer和upload模塊:
layui.use(['layer', 'upload'], function () {
$ = layui.jquery;
upload = layui.upload;
layer = layui.layer
4、執(zhí)行實(shí)例,接口配置:
var uploadInst = upload.render({ elem: '#img_upload' //綁定元素 , url: '/Files/upload' //上傳接口, field: 'image', done: function (res) {
//上傳完畢回調(diào)
if (res.code > 0) {
return layer.alert(res.msg, { icon: 2 });
}
$('#img_singer').attr('src', res.data);
}
, error: function () {
//請(qǐng)求異常回調(diào)
}
});
});
5、創(chuàng)建Files類及upload方法:
function upload(){
//接收數(shù)據(jù):
$file = request()->file('image');
$savename = '/storage/' . \think\facade\Filesystem::disk('public')->putFile('topic', $file);
//同一斜杠:
$savename = str_replace('\\', '/', $savename);
//返回響應(yīng)結(jié)果:
return json(['code' => 0, 'data' => $savename]);
// 可以echo也可以return
}
存儲(chǔ)上傳數(shù)據(jù):
function save() {
var data = {};
data.name = $('input[name="title"]').val();
data.catid = $('select[name="catid"]').val();
data.avatar = $('#img_singer').attr('src');
console.log(data);
$.post('/singer/save', data, function (res) {
console.log(res);
if (res) {
layer.alert('新增成功', { icon: 1 }, function () {
location.href = 'http://admin.phpenv.com/';
});
}
}, 'json')
}
$url = "http://v.juhe.cn/toutiao/index?type=guoji&key=***";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPGET, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($curl, CURLOPT_HEADER, false);
// 執(zhí)行curl_exec()獲取到的信息不輸出,直接已字符串方式返回
curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);
$apiData = curl_exec($curl);
// echo $apiData;
$data = json_decode($apiData, true);
curl的參數(shù)配置有點(diǎn)看不懂,根據(jù)老師的配置可以獲取網(wǎng)絡(luò)數(shù)據(jù),有點(diǎn)像爬蟲;通過配置layui實(shí)現(xiàn)文件上傳比較容易理解,注意第1條文件配置,否則會(huì)報(bào)錯(cuò)。
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)