chengkun
2025-04-18 1bb985f32f2efe0f9dd69f3cf29a1c809b1cf96d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
<?php
/**
 * $Author :PHPYUN开发团队
 *
 * 官网: http://www.phpyun.com
 *
 * 版权所有 2009-2021 宿迁鑫潮信息技术有限公司,并保留所有权利。
 *
 * 软件声明:未经授权前提下,不得用于商业运营、二次开发以及任何形式的再次发布。
 */
 
/**
 *  注意设置网站服务器的php.ini以下两项为合适的值:(一般通过现在的手机手机拍照,照片大小都好几兆,可能大于2M)
 *    upload_max_filesize = 8m ; 允许上传文件大小的最大值。默认为2M
 *    post_max_size = 8m ; 通过表单POST给PHP的所能接收的最大值,包括表单里的所有值。默认为8M
 */
 
class upload_model extends model{
 
    private $watermark = 1; //watermark :0无视后台的水印开关不添加水印,只用于后台上传水印图片时,以及pc上传头像裁剪保存之前
 
    /**
     *
     * @param array $data | file/上传文件 ;  dir/上传路径;  type/上传用途(logo表示头像);  base/base4图片;  preview/pc预览即上传;
                            thumb缩率图参数数组,包含width,height,newNamePre,addpreview ;
                            watermark :本类私有属性watermark设置
     * @return array|mixed|string
     */
    function newUpload($data = array('file' => null, 'dir' => null, 'type' => null, 'base' => null, 'preview' => null, 'thumb' => array(), 'watermark' => 1))
    {
 
        $return     =   array();
        $pictype    =   isset($data['type']) ? $data['type'] : '';
        if (isset($data['watermark'])) {
 
            $this->watermark    =   $data['watermark'];
        }
 
        if (isset($this->config['sy_oss']) && $this->config['sy_oss'] == 1) {
            if ($data['dir'] == 'logo') {
 
                $dir=   'data/logo';
            } else {
 
                $dir=   'data/upload/' . $data['dir'];
            }
            $upload =   $this->ossUpload();
 
            if ($data['file']) {
 
                $thumb  =   array();
                // 分辨率限制条件
                $fbl    =   1920;
                $imginfo=   getimagesize($data['file']['tmp_name']);
 
                if ($imginfo[0] > $fbl || $imginfo[1] > $fbl) {
                    $thumb['crop']  =   $fbl;
                }
                if ($pictype == 'logo') {   // logo图片太大,需要进行压缩
                    if ($imginfo[0] > 300 || $imginfo[1] > 300) {
 
                        $thumb['crop']  =   300;
                    }
                }
                $return =   $upload->uploadImg($data['file'], $dir, $thumb);
            } elseif ($data['base']) {
 
                $return =   $upload->imageBase($data['base'], $dir);
            }
        } else {
 
            if ($data['dir'] == 'logo') {   // 后台上传网站logo等
 
                $dir    =   APP_PATH . 'data/logo/';
            } else {
 
                $dir    =   APP_PATH . 'data/upload/' . $data['dir'] . '/';
            }
 
            $upload     =   $this->Upload_pic($dir);
 
            if (!empty($data['base'])) {
 
                $pic    =   $upload->imageBase($data['base']);
            } else {
 
                $pic    =   $upload->picture($data['file'], $pictype);
            }
 
            // 匹配错误信息
            $picmsg     =   $this->picmsg($pic);
 
            if (!empty($picmsg['msg'])) {
 
                $return['msg']  =   '上传失败:' . $picmsg['msg'];
            } else {
                if ($pictype == 'thumb') {
 
                    $return['picurl']   =   $upload->makeThumb($pic, $data['thumb']['width'], $data['thumb']['height'], $data['thumb']['newNamePre'], $data['thumb']['addpreview']);
                } elseif ($pictype == 'news') {
 
                    //后台新闻,需要原图加缩率图
                    $return['picurl']   =   $pic;
                    $return['thumburl'] =   $upload->news_makeThumb($pic, 200, 133, '_S_');
                } else {
 
                    $return['picurl']   =   $pic;
                }
                if ($data['dir'] == 'logo') {
 
                    // 后台上传网站logo等
                    $return['picurl']   =   str_replace(APP_PATH . 'data', 'data', $return['picurl']);
                } else {
 
                    $return['picurl']   =   str_replace(APP_PATH . 'data', './data', $return['picurl']);
 
                    if (!empty($return['thumburl'])) {
 
                        $return['thumburl'] =   str_replace(APP_PATH . 'data', './data', $return['thumburl']);
                    }
                }
            }
        }
        return $return;
    }
 
    function voiceUpload($data = array('file' => null, 'dir' => null, 'type' => null))
    {
 
        $return =   array();
 
        if (isset($this->config['sy_oss']) && $this->config['sy_oss'] == 1) {
 
//            $dir    =   'data/upload/' . $data['dir'];
 
            $upload =   $this->ossUpload();
 
            if ($data['file']) {
 
                $return =   $upload->uploadVoice($data['file']);
            }
        } else {
 
//            $varr       =   pathinfo($data['file']['name']);
            $newName    =   time().rand(1000, 9999);
            $upfiledir  =   APP_PATH.'data/upload/voice' . '/' . date('Ymd') . '/';
            if (!file_exists($upfiledir)) {
                @mkdir($upfiledir, 0777, true);
            }
 
            $vdir       =   $upfiledir.$newName.'.mp3';
 
            if (move_uploaded_file($data['file']['tmp_name'], $vdir)) {
 
                $voiceDir           =   str_replace(APP_PATH . 'data', 'data', $vdir);
                $return['voiceurl'] =   $voiceDir;
            } else {
 
                $return['error']    =   2;
                $return['msg']      =   '语音保存出错,请重试';
            }
        }
        return $return;
    }
 
    function ossUpload()
    {
 
        include_once(LIB_PATH.'oss/ossupload.class.php');
        $paras  =   $this->paras();
        $upload =   new ossUpload($paras);
        return $upload;
    }
 
    /**
     * @desc    上传方法
     * @param   string $dir
     * @param   string $destination_folder
     * @return  Upload
     */
    function Upload_pic($dir = '', $destination_folder = '')
    {
 
        include_once(LIB_PATH.'upload.class.php');
 
        $paras  =   $this->paras(array('dir' => $dir, 'destination_folder' => $destination_folder));
 
        //  判断后台水印上传设置
        if ($this->watermark == '1' && $this->config['is_watermark'] == 1) {
 
            $paras['addwatermark']  =   true;
            $paras['position']      =   $this->config['wmark_position'] ? $this->config['wmark_position'] : 1;  //水印位置
            $paras['waterimg']      =   APP_PATH.$this->config['sy_watermark']; //水印图片
        } else {
 
            $paras['addwatermark']  =   false;
        }
 
        $upload =   new Upload($paras);
        return $upload;
    }
 
    private function paras($data = array())
    {
 
        if (isset($data['dir'])) {
 
            $paras['upfiledir'] =   $data['dir'];//上传路径
        }
 
        //根据后台配置 读取上传限定大小
        if ($this->config['pic_maxsize']) {
 
            $paras['maxsize']   =   (int)$this->config['pic_maxsize'] * 1024;
        } else {
 
            $paras['maxsize']   =   5 * 1024;
        }
 
        //传入设定 图片类型
        if ($this->config['pic_type']) {
 
            $this->config['pic_type']   =   str_replace('.', '', $this->config['pic_type']);
 
            $paras['pic_type']  =   explode(',', str_replace(' ', '', $this->config['pic_type']));
        } else {
 
            $paras['pic_type']  =   array('jpg', 'png', 'jpeg', 'bmp', 'gif');
        }
 
        //禁止后台设定可执行程序后缀
        foreach ($paras['pic_type'] as $pickey => $picvalue) {
 
            $pic_type   =   strtolower(str_replace('.', '', trim($picvalue)));
            if (in_array($pic_type, array('php', 'asp', 'aspx', 'jsp', 'exe', 'do'))) {
 
                unset($paras['pic_type'][$pickey]);
            }
        }
        //判断是否需要进行图片验证
        $paras['is_picself']    =   $this->config['is_picself'];
 
        //判断是否需要强制压缩
        if ($this->config['is_picthumb'] == 1) {
 
            $paras['addpreview'] = true;//是否生成缩略图
        } else {
 
            $paras['addpreview'] = false;//是否生成缩略图
        }
 
        if (isset($data['destination_folder']) && $data['destination_folder'] != '') {
 
            $paras['destination_folder']    =   $data['destination_folder'];
        }
 
        return $paras;
    }
 
    function picmsg($status)
    {
 
        $error  =   array('1' => '文件太大', '2' => '文件类型不符', '3' => '同名文件已经存在', '4' => '移动文件出错,请检查upload目录权限', '6' => '非法文件,无法上传');
 
        if (isset($error[$status])) {
 
            $data['status'] =   $status;
            $data['msg']    =   $error[$status];
            return $data;
        } else {
 
            return true;
        }
    }
 
    /**
     * pc预览直接上传、保存图片
     * @param array $data
     * @return array
     */
    function layUpload($data = array())
    {
 
 
        $path   =   isset($data['path']) ? $data['path'] : '';
        $upArr  =   array(
 
            'file'  =>  $data['file'],
            'dir'   =>  $path,
        );
 
        if (!empty($data['uid']) && !empty($data['usertype']) && !empty($data['imgid']) && $path != 'logo') {
            // 个人用户
            if ($data['usertype'] == 1) {
 
                require_once('resume.model.php');
                $resumeM    =   new resume_model($this->db, $this->def);
 
                if ($data['imgid'] == 'logo') { // 上传头像
 
                    $this->watermark = 0;
                    $result =   $resumeM->upPhoto(array('uid' => $data['uid']), array('photo' => $data['file'], 'utype' => 'user', 'preview' => 1));
                }
 
                if ($data['imgid'] == 'ewm') {  // 上传二维码
 
                    $result =   $resumeM->upEwm(array('uid' => $data['uid']), array('photo' => $data['file'], 'utype' => 'user', 'preview' => 1));
                }
            }
            // 企业用户
            if ($data['usertype'] == 2) {
 
                require_once('company.model.php');
                $companyM   =   new company_model($this->db, $this->def);
 
                if ($data['imgid'] == 'logo') { // 上传logo
 
                    $this->watermark=   0;
 
                    if (isset($data['notoken']) && $data['notoken'] == 1) {
 
                        $result     =   $companyM->upLogo(array('uid' => $data['uid']), array('photo' => $data['file'], 'preview' => 1));
                    } else {
 
                        $result     =   $companyM->upLogo(array('uid' => $data['uid']), array('photo' => $data['file'], 'utype' => 'user', 'preview' => 1));
                    }
                } elseif ($data['imgid'] == 'ewm') { // 上传二维码
 
                    $result         =   $companyM->upEwm(array('uid' => $data['uid']), array('photo' => $data['file'], 'utype' => 'user', 'preview' => 1));
                }
            }
            //猎头
            if ($data['usertype'] == 3) {
 
                require_once('lietou.model.php');
                $lietouM    =   new lietou_model($this->db, $this->def);
 
                if ($data['imgid'] == 'logo') { // 上传logo
 
                    $this->watermark = 0;
                    $result =   $lietouM->upLogo(array('uid' => $data['uid']), array('photo' => $data['file'], 'utype' => 'user', 'preview' => 1));
                }
            }
            //培训
            if ($data['usertype'] == 4) {
 
                require_once('train.model.php');
                $trainM     =   new train_model($this->db, $this->def);
 
                if ($data['imgid'] == 'logo') { // 上传logo
 
                    $this->watermark = 0;
                    $result =   $trainM->upLogo(array('uid' => $data['uid']), array('photo' => $data['file'], 'utype' => 'user', 'preview' => 1));
                }
            }
            //自由
            if ($data['usertype'] == 5) {
 
                require_once('gqdemand.model.php');
                $gqdemandM  =   new gqdemand_model($this->db, $this->def);
 
                // 上传logo
                if ($data['imgid'] == 'logo') {
 
                    $this->watermark = 0;
                    $result =   $gqdemandM->upLogo(array('uid' => $data['uid']), array('photo' => $data['file'], 'utype' => 'free', 'preview' => 1));
                }
            }
        } elseif (!empty($data['name']) && $data['path'] == 'logo') {
 
            //if ($data['name'] == 'sy_watermark') {
 
                $this->watermark = 0;
            //}
 
            // 后台上传网站logo
            $result =   $this->newUpload($upArr);
 
            $pic    =   $result['picurl'];
 
            $result['picurl']   =   checkpic($pic);
            $post[$data['name']]=   $pic;
 
 
            //小程序封面图/微信关注欢迎图 需要单独上传至微信素材库
            if ($data['name'] == 'sy_xcxpic') {
 
                require_once('weixin.model.php');
                $wxM    =   new weixin_model($this->db, $this->def);
 
                $upMedia=   $wxM->upMedia($pic);
 
                if (!$upMedia['media_id']) {
 
                    $result['errcode']  =   8;
                    $result['msg']      =   '封面素材上传失败!' . $upMedia['errmsg'];
                } else {
 
                    $post['sy_xcxmedia']        =   $upMedia['media_id'];
                }
            } elseif ($data['name'] == 'sy_wxcom_pic') {
 
                require_once('weixin.model.php');
                $wxM    =   new weixin_model($this->db, $this->def);
 
                $upMedia=   $wxM->upMedia($pic);
 
                if (!$upMedia['media_id']) {
 
                    $result['errcode']  =   8;
                    $result['msg']      = '欢迎图素材上传失败!' . $upMedia['errmsg'];
                } else {
 
                    $post['sy_wxcom_picmedia']  =   $upMedia['media_id'];
                }
            }
 
            require_once('config.model.php');
            $configM    =   new config_model($this->db, $this->def);
            $configM->setConfig($post);
        }
 
        if (!empty($result['status']) || (isset($result['errcode']) && $result['errcode'] == 8)) {
 
            $return     =   array(
 
                'code'  =>  1,
                'msg'   =>  $result['msg'],
                'data'  =>  array()
            );
        } else {
 
            $return     =   array(
 
                'code'  =>  0,
                'msg'   =>  $result['msg'],
                'data'  =>  array('url' => $result['picurl'])
            );
        }
        return $return;
    }
 
    /**
     * pc头像剪裁
     * @param $dir
     * @return mixed
     */
    public function thumb($dir)
    {
        if (isset($this->config['sy_oss']) && $this->config['sy_oss'] == 1) {
            if ($dir) {
 
                $data['dir'] = 'data/upload/' . $dir;
            }
 
            $upload =   $this->ossUpload();
 
            if (isset($_POST['x']) && isset($_POST['y'])) {
 
                $pictures   =   $upload->cutPic($_POST['img1'], $data['dir'], $_POST['x'], $_POST['y'], $_POST['width'], $_POST['height'], $_POST['scale']);
                $thumb[1]   =   $pictures['picurl'];
            } else {
 
                $thumb[1]   =   '../data/upload/' . $dir . '/' . date('Ymd') . '/' . end(explode('/', $_POST['img1']));
            }
        } else {
 
            include LIB_PATH . 'sizer.class.php';
            $sizer  =   new Sizer('../data/upload/' . $dir . '/' . date('Ymd') . '/', $this->config['sy_ossurl']);
            $thumb  =   $sizer->sizeIt();
        }
        return $thumb;
    }
 
    /**
     * @desc    thumb缩率图参数数组,包含width,height,newNamePre,addpreview
     * @param   null[] $data | file/上传文件 ;  dir/上传路径;
     * @return  mixed
     */
    public function uploadDoc($data = array('file'=>null,'dir'=>null))
    {
 
        $dir    =   'data/upload/' . $data['dir'];
 
        if (isset($this->config['sy_oss']) && $this->config['sy_oss'] == 1) {
 
            $upload =   $this->ossUpload();
            $return =   $upload->uploadDoc($data['file'], $dir);
 
            if ($data['dir'] == 'comdoc' && isset($return['docurl'])) {
 
                // 上传委托书范本的,人才网服务器要传一份,不然小程序因安全域名问题,无法下载
                $arr    =   parse_url($this->config['sy_weburl'] . $return['docurl']);
                $parr   =   pathinfo($arr['path']);
 
                $this->saveLocalFile($dir, $data, $parr['filename']);
            }
        } else {
 
            $return =   $this->saveLocalFile($dir, $data);
        }
        return $return;
    }
 
    /**
     * @desc    保存文件到服务器
     * @param   $dir
     * @param   $data
     * @param   string $filename
     * @return  mixed
     */
    function saveLocalFile($dir, $data, $filename = '')
    {
        if (!is_dir(APP_PATH . $dir . '/' . date('Ymd'))) {
 
            mkdir(APP_PATH . $dir . '/' . date('Ymd'), 0777, true);
        }
 
        $nametype   =   @explode('.', trim($data['file']['name']));
 
        $filetype   =   strtolower(end($nametype));
 
        if ($this->config['file_type']) {
 
            $this->config['pic_type']   =   str_replace('.', '', $this->config['file_type']);
 
            $file_type  =   explode(',', str_replace(' ', '', $this->config['file_type']));
 
            //禁止后台设定可执行程序后缀
            foreach ($file_type as $filekey => $filevalue) {
 
                $new_file_type  =   strtolower(str_replace('.', '', trim($filevalue)));
 
                if (in_array($new_file_type, array('php', 'asp', 'aspx', 'jsp', 'exe', 'do'))) {
 
                    unset($file_type[$filekey]);
                }
            }
        } else {
 
            $file_type  =   array('jpg', 'png', 'jpeg', 'bmp', 'gif');
        }
 
 
        if (!in_array($filetype, $file_type)) {//检查文件类型
 
            $return['msg']  =   '禁止上传' . $filetype . '类型文件!';
        } else {
            if (empty($filename)) {
 
                $filename   =   time() . rand(1000, 9999);
            }
            $upload     =   $dir . '/' . date('Ymd') . '/' . $filename . '.' . $filetype;
 
            $pathname   =   APP_PATH . $upload;
 
            $result     =   move_uploaded_file($data['file']['tmp_name'], $pathname);
 
            if ($result) {
 
                $return['docurl']   =   '/' . $upload;
            } else {
 
                $return['msg']      =   '文件上传失败,请检查DATA目录权限!';
            }
        }
        return $return;
    }
 
}
?>