Files
liche/admin/controllers/Upload.php
T
2022-03-25 18:05:03 +08:00

151 lines
5.1 KiB
PHP
Executable File

<?php
/**
* Created by PhpStorm.
* User: linfan
* Date: 2018/11/13
* Time: 16:43
*/
defined('BASEPATH') OR exit('No direct script access allowed');
ini_set('display_errors', 'On');
class Upload extends CI_Controller
{
public function index()
{
$mode = $this->input->get('mode');
$types = $this->input->get('type');
$uptype = $this->input->get('uptype');
$field = $this->input->get('field');
$mark = $this->input->get('mark');
$source = $this->input->get('source');
if('file' == $mark){
return $this->pdf();
}
$data['mode'] = $mode ? $mode : '1';
$data['types'] = $types ? $types : 'jpg,png';
$data['uptype'] = $uptype ? $uptype : 'qiniu';
$data['mimes'] = $this->file_mime($data['types']);
$data['field'] = $field ? $field : 'file';
$data['source'] = $source ? $source : '';
if ($mark) {
$mark_data = explode(':', $mark);
$data['mark'] = $mark_data[0];
$data['mark_id'] = $mark_data[1];
$data['mark_url'] = count($mark_data) >= 3 ? $mark_data[2] : '/biz/store/pic/add';
}
return $this->load->view('upload', $data);
}
public function pdf(){
$mode = $this->input->get('mode');
$types = $this->input->get('type');
$uptype = $this->input->get('uptype');
$source = $this->input->get('source');
$field = $this->input->get('field');
$data['mode'] = $mode ? $mode : '1';
$data['types'] = $types;
$data['uptype'] = $uptype ? $uptype : 'qiniu';
$data['mimes'] = $this->file_mime($data['types']);
$data['source'] = $source ? $source : '';
$data['field'] = $field ? $field : '';
$this->load->view('uploadpdf', $data);
}
//文件状态检查
public function upstate()
{
$post = $this->input->post();
$this->load->library('qiniu');
$config = $this->qiniu->getConfig();
$filename = join('/', str_split($post['md5'], 16)) . '.' . strtolower(pathinfo($post['filename'], PATHINFO_EXTENSION));
$filename = $config['attch'] . date('Y/m') . '/' . $filename;
$ratio = number_format($post['height']/$post['width'], 1);
$ratio_exm = number_format(4/3, 1);
if ($ratio <= 0.3){
$picHeight = 225;
} else if(0.3 < $ratio && $ratio < $ratio_exm) {
$picHeight = number_format($post['height']/$post['width'], 2) * 750;
} else if($ratio >= $ratio_exm) {
$picHeight = 1000;
}
// 检查文件是否已上传
if ($site_url = $this->qiniu->getFileUrl($filename)) {
return $this->show_json('IS_FOUND', array(
'site_url' => $site_url, 'file_url' => $filename, 'picHeight' => $picHeight, 'width' => $post['width']
));
}
// 需要上传文件,生成上传配置参数
$config = array('uptype' => $post['uptype'], 'file_url' => $filename);
$config['server'] = $this->qiniu->getUploadQiniuUrl(true);
$config['token'] = $this->qiniu->getToken($filename);
$config['picHeight'] = $picHeight;
return $this->show_json('NOT_FOUND', $config);
}
//文件状态检查
public function upstate_pdf()
{
$post = $this->input->post();
$this->load->library('qiniu');
$config = $this->qiniu->getConfig();
$filename = join('/', str_split($post['md5'], 16)) . '.' . strtolower(pathinfo($post['filename'], PATHINFO_EXTENSION));
$filename = $config['attch'] . date('Y/m') . '/' . $filename;
// 检查文件是否已上传
if ($site_url = $this->qiniu->getFileUrl($filename)) {
return $this->show_json('IS_FOUND', array(
'site_url' => $site_url,
'file_url' => $filename,
'file_title' => substr($filename, strrpos($filename, '/')+1)
));
}
// 需要上传文件,生成上传配置参数
$config = array('uptype' => $post['uptype'], 'file_url' => $filename);
$config['server'] = $this->qiniu->getUploadQiniuUrl(true);
$config['token'] = $this->qiniu->getToken($filename);
$config['file_title'] = substr($filename, strrpos($filename, '/')+1);
return $this->show_json('NOT_FOUND', $config);
}
//返回json数据
protected function show_json($code = '', $data = array())
{
header('Content-Type:application/json; charset=utf-8');
echo json_encode(array('code' => $code, 'data' => $data), JSON_UNESCAPED_UNICODE);
return false;
}
private function file_mime($ext, $mime = array())
{
$mimes = &get_mimes();
foreach (is_string($ext) ? explode(',', $ext) : $ext as $e) {
$tmp = isset($mimes[strtolower($e)]) ? $mimes[strtolower($e)] : 'application/octet-stream';
if (is_array($tmp)) {
$mime = array_merge($mime, $tmp);
} else {
$mime[] = $tmp;
}
}
return join(',', array_unique($mime));
}
}