Skip to content
On this page

服务端图片上传逻辑(PHP)

$file = request()->file('file');
if($file){
    $info = $file->validate(['size'=> 512000,'ext'=>'jpg,png,jpeg'])->move(ROOT_PATH . 'public' . DS . 'uploads');
    if($info){
      $data = [
        'src' => request()->domain() . DS . 'uploads' . DS . $info->getSaveName(),
        'title' => $info->getFilename(),
       ];
      return json(['code' => 0, 'status' => true, 'msg' => '', 'data' => $data]);
    }
}

return json(['status' => false, 'msg' => $file->getError()]);
1
2
3
4
5
6
7
8
9
10
11
12
13

tp5.0 上传文件到Oss

  • Composer 安装OSS,项目目录下执行命令
composer require aliyuncs/oss-sdk-php
1
  • 默认存储目录
项目名称/vender/
1
  • 控制器中引用
use OSS\OssClient;
1
  • 文件上传,OSS各参数阿里云后台获取
$ossClient = new OssClient($this->aliOos['KeyId'], $this->aliOos['KeySecret'], $this->aliOos['Endpoint']);
$file = request()->file('file');
if($file){
    $file_name = $file->getInfo('name');
    $file_ext  = $this->getExt($file_name);
    $file_path = '目录' . DIRECTORY_SEPARATOR . '自定义目录(没有会自动创建)' .  DIRECTORY_SEPARATOR . date('Ymd') . DIRECTORY_SEPARATOR . time() . rand(10000000, 99999999) . '.' . $file_ext;
    $imgpath   = str_replace('\\', '/', $file_path);
    $url       = $ossClient->uploadFile($this->aliOos['Bucket'], $imgpath, $_FILES['file']['tmp_name']);
    if ($url['info']) {
        // oss网址替换已绑定域名
        $ossImgUrl = str_replace('http://***.aliyuncs.com/', 'http://***erha.love/', $url['info']['url']);
        return ['status' => true, 'code' => 200, 'msg'=>'上传成功', 'local_data' => $ossImgUrl];
    }
} 

// 上传失败获取错误信息
return ['status' => false, 'code' => 500, 'msg'=>'上传失败', 'local_data' => '', 'error_msg' => $file->getError()];

// 获取文件后缀
private function getExt($filename)
{
    $arr = explode('.', $filename);
    return array_pop($arr);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

tp5.0 自带分页函数paginate()

  • 查询状态为1的用户数据 并且每页显示10条数据
# 保持地址栏携带的参数在点击分页时不被清空
['query' => request()->param()]
$lists = Db::name('user')->where('status',1)->paginate(10, false, ['query' => request()->param()]);
1
2
3
  • 模板文件中分页及总条数输出代码如下
# 分页
{$lists->render()}
# 总条数
{$lists->total()}
1
2
3
4

tp5.0 extra文件夹 文件数据获取

  • 在application/extra下的.php文件,控制器中可用助手函数config('文件名')获取内容
config('文件名')
1

thinkphp5.0 json()和原生json_encode()

  • 按需所用
thinkphp5.0 自带的json()函数返回数据类型是json对象。
PHP原生的json_encode()返回的数据类型是json字符串。
1
2

微信JSAPI网页支付 商户支付应答(v3和v2)

微信JSAPI网页支付V3(URL请求版)大致流程

APPID(公众号或服务号ID)、mchid(商户号)、APIKey(微信商户API密钥)、API密钥(公众号或服务号密钥)
1