1
0
Files
helper/application/mobile/controller/Upload.php
2020-08-06 14:58:51 +08:00

76 lines
2.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +------------------------------------------------+
// |http://www.cjango.com |
// +------------------------------------------------+
// | 修复BUG不是一朝一夕的事情等我喝醉了再说吧 |
// +------------------------------------------------+
// | Author: 小陈叔叔 <Jason.Chen> |
// +------------------------------------------------+
namespace app\mobile\controller;
use think\Config;
use think\Db;
class Upload extends _Init
{
public function uploadPicture()
{
$data = $this->request->post('files.base64');
$size = $this->request->post('files.size');
list($type, $data) = explode(',', $data);
if (strstr($type, 'image/jpeg') !== '') {
$ext = '.jpg';
} elseif (strstr($type, 'image/gif') !== '') {
$ext = '.gif';
} elseif (strstr($type, 'image/png') !== '') {
$ext = '.png';
}
$fileN = 'img' . time() . $ext;
$path = Config::get('upload.rootPath') . 'image/';
if (!$this->mkdir($path)) {
return $this->error('头像保存目录不存在');
}
$fileName = $path . $fileN;
file_put_contents($fileName, base64_decode($data));
$path = substr($fileName, 1);
$data = [
'type' => 'image',
'name' => $fileN,
'ext' => $ext,
'hash' => md5($fileN),
'path' => $path,
'size' => $size,
'create_time' => time(),
];
$last = Db::name('Storage')->insertGetId($data);
if ($last) {
$re = [
'filename' => $path,
'fileid' => $last,
];
return $this->success('上传成功', '', $re);
} else {
return $this->error('上传失败');
}
}
/**
* [mkdir 检测路径]
* @param [type] $savepath [description]
*/
private function mkdir($dir)
{
if (is_dir($dir)) {
return true;
}
if (mkdir($dir, 0755, true)) {
return true;
} else {
return false;
}
}
}