<?php
namespace Api\Controller;
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods' 'GET, POST'");
use Think\Controller;
class ImgUploadController extends Controller
{
public $uploadRoute;
public $uploadDocx;
public function __construct()
{
parent::__construct();
//图片文件路径
$this->uploadRoute = "/Public/Uploads/Industry/" . date("Y") . '/' . date("m") . '/' . date("d") . '/';
//文档上传路径
$this->uploadDocx = "/Public/Uploads/Authority/" . date("Y") . '/' . date("m") . '/' . date("d") . '/';
}
public function index()
{
foreach ($_FILES as $key => $v) {
//文件类型
$type = end(explode('.', $v['name']));
}
$time = time();
$getRandomString = $this->getRandomString(8);
$upload = new \Think\Upload(); // 实例化上传类
$upload->maxSize = 471859; // 设置附件上传大小
$upload->saveName = array('uniqid', $time . $getRandomString);
// $upload->exts = array('jpg', 'gif', 'png', 'jpeg','docx');// 设置附件上传类型
$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
$upload->saveExt = 'jpg';
$this->getFile($type); //创建文件夹
if ($type == 'docx') {
$upload->rootPath = $_SERVER['DOCUMENT_ROOT'] . $this->uploadDocx; // 设置附件上传根目录
} else {
$upload->rootPath = $_SERVER['DOCUMENT_ROOT'] . $this->uploadRoute; // 设置附件上传根目录
}
$upload->savePath = ''; // 设置附件上传(子)目录
// 上传文件
$info = $upload->upload();
if (!$info) {// 上传错误提示错误信息
echo json_encode(['code' => 2, 'msg' => $upload->getError(), 'url' => $url2]);
exit;
} else {// 上传成功
foreach ($info as $file) {
$url = $file['savepath'] . $file['savename'];
if ($type == 'docx') {
$url2 = $this->uploadDocx . $url; // 设置附件上传根目录
} else {
$url2 = $this->uploadRoute . $url; // 设置附件上传根目录
}
}
echo json_encode(['code' => 1, 'msg' => '上传成功', 'url' => $url2]);
exit;
}
}
//创建文件夹
public function getFile($type)
{
if ($type == 'docx') {
$dest_dir = dirname(dirname(dirname(dirname(__FILE__)))) . $this->uploadDocx;
if (!is_dir($dest_dir) || !is_writeable($dest_dir)) {
$dest_dir1 = dirname(dirname(dirname(dirname(__FILE__)))) . '/Public/Uploads/Authority/' . date("Y");
$dest_dir2 = dirname(dirname(dirname(dirname(__FILE__)))) . '/Public/Uploads/Authority/' . date("Y") . '/' . date("m");
$dest_dir3 = dirname(dirname(dirname(dirname(__FILE__)))) . $this->uploadDocx;
mkdir($dest_dir1, 0777, true);
chmod($dest_dir1, 0777);
mkdir($dest_dir2, 0777, true);
chmod($dest_dir2, 0777);
mkdir($dest_dir3, 0777, true);
chmod($dest_dir3, 0777);
}
} else {
$dest_dir = dirname(dirname(dirname(dirname(__FILE__)))) . $this->uploadRoute;
if (!is_dir($dest_dir) || !is_writeable($dest_dir)) {
$dest_dir1 = dirname(dirname(dirname(dirname(__FILE__)))) . '/Public/Uploads/Industry/' . date("Y");
$dest_dir2 = dirname(dirname(dirname(dirname(__FILE__)))) . '/Public/Uploads/Industry/' . date("Y") . '/' . date("m");
$dest_dir3 = dirname(dirname(dirname(dirname(__FILE__)))) . $this->uploadRoute;
mkdir($dest_dir1, 0777, true);
chmod($dest_dir1, 0777);
mkdir($dest_dir2, 0777, true);
chmod($dest_dir2, 0777);
mkdir($dest_dir3, 0777, true);
chmod($dest_dir3, 0777);
}
}
return true;
}
public function phoneImg()
{
$base64_string = $_POST['base64_string'];
$savename = time() . mt_rand(5, 10) . uniqid() . $this->getRandomString(8) . '.jpg';//localResizeIMG压缩后的图片都是jpeg格式
$dest_dir = $_SERVER['DOCUMENT_ROOT'] . "/Public/Uploads/Phone/" . date("Y") . '/' . date("m") . '/' . date("d");
if (!is_dir($dest_dir) || !is_writeable($dest_dir)) {
$dest_dir1 = $_SERVER['DOCUMENT_ROOT'] . '/Public/Uploads/Phone/' . date("Y");
$dest_dir2 = $_SERVER['DOCUMENT_ROOT'] . '/Public/Uploads/Phone/' . date("Y") . '/' . date("m");
$dest_dir3 = $_SERVER['DOCUMENT_ROOT'] . '/Public/Uploads/Phone/' . date("Y") . '/' . date("m") . '/' . date("d");
mkdir($dest_dir1, 0777, true);
chmod($dest_dir1, 0777);
mkdir($dest_dir2, 0777, true);
chmod($dest_dir2, 0777);
mkdir($dest_dir3, 0777, true);
chmod($dest_dir3, 0777);
}
$savepath = $_SERVER['DOCUMENT_ROOT'] . "/Public/Uploads/Phone/" . date("Y") . '/' . date("m") . '/' . date("d").'/'.$savename; //上传的路径
$image = self::base64_to_img($base64_string, $savepath);
$url = '/Public/Uploads/Phone/'.date("Y").'/'.date("m").'/'.date("d").'/' . $savename;
//$url2 = getRequestHTTP() . $_SERVER['HTTP_HOST'] . $url;
$this->ajaxReturn(['code' => 1, 'msg' => '上传成功', 'url' => $url]);
}
/*富友上传图片fuiou
图片类型 中文命名 ZIP包中实际文件名
营业执照照片 证件照片.jpg d6a4bcfed5d5c6ac.jpg
商户标准协议照片 商户协议左.jpg c9ccbba7d0add2e9d7f3.jpg
商户增值协议照片 商户协议右.jpg c9ccbba7d0add2e9d3d2.jpg
法人身份证正面照片 法人身份证明正面.jpg b7a8c8cbc9edb7ddd6a4c3f7d5fdc3e6.jpg
法人身份证反面照片 法人身份证明反面.jpg b7a8c8cbc9edb7ddd6a4c3f7b7b4c3e6.jpg
组织机构代码证照片 组织机构代码证.jpg d7e9d6afbbfab9b9b4fac2ebd6a4.jpg
税务登记证照片 税务登记证.jpg cbb0cef1b5c7bcc7d6a4.jpg
对私账户身份证正面照片 对私账户身份证明正面.jpg b6d4cbbdd5cbbba7c9edb7ddd6a4c3f7d5fdc3e6.jpg
对私账户身份证反面照片 对私账户身份证明背面.jpg b6d4cbbdd5cbbba7c9edb7ddd6a4c3f7b1b3c3e6.jpg
入账银行卡正面照片 银行卡正面.jpg d2f8d0d0bfa8d5fdc3e6.jpg
入账非法人证明照片(填写关系证明表格并拍照) 入账非法人证明.jpg c8ebd5cbb7c7b7a8c8cbd6a4c3f7.jpg
开户许可证照片 开户许可证.jpg bfaabba7d0edbfc9d6a4.jpg
门头照片 门头照片.jpg c3c5cdb7d5d5c6ac.jpg
门脸照片 门脸照片.jpg c3c5c1b3d5d5c6ac.jpg
手持身份证照片 手持身份证照片.jpg cad6b3d6c9edb7ddd6a4d5d5c6ac.jpg
内设照片 内设照片.jpg c4dac9e8d5d5c6ac.jpg
*/
public function fyUpload()
{
foreach ($_FILES as $key => $v) {
$type = end(explode('.', $v['name'])); //文件类型
}
$img_name = $_POST['img_name'];
$merchant_num =$_POST['merchant_num'];
$upload_path = "/Public/Uploads/Fuyou/".$merchant_num."/";
if(!$img_name || !$merchant_num){
echo json_encode(['code' => 2, 'msg' => '文件名称或门店编号不能为空']);
exit;
}
$upload = new \Think\Upload(); // 实例化上传类
$upload->maxSize = 471859; // 设置附件上传大小
$upload->saveName = $img_name;
$upload->replace = true;
$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
$upload->saveExt = 'jpg';
//判断文件是否存在创建文件
$dest_dir = $_SERVER['DOCUMENT_ROOT'].$upload_path;
if (!is_dir($dest_dir) || !is_writeable($dest_dir)) {
$dest_dir1 = $_SERVER['DOCUMENT_ROOT'].$upload_path;
mkdir($dest_dir1, 0777, true);
chmod($dest_dir1, 0777);
}
$upload->rootPath = $_SERVER['DOCUMENT_ROOT'].$upload_path; // 设置附件上传根目录
$upload->savePath = ''; // 设置附件上传(子)目录
// 上传文件
$info = $upload->upload();
if (!$info) {// 上传错误提示错误信息
echo json_encode(['code' => 2, 'msg' => $upload->getError(), 'url' => $url2]);
exit;
} else {// 上传成功
foreach ($info as $file) {
$url = $file['savepath'] . $file['savename'];
$url2 = $upload_path. $url; // 设置附件上传根目录
}
echo json_encode(['code' => 1, 'msg' => '上传成功', 'url' => $url2]);
exit;
}
}
/*
* 获取64位文件
* */
public function base64_to_img($base64_string, $output_file)
{
$ifp = fopen($output_file, "wb");
fwrite($ifp, base64_decode($base64_string));
fclose($ifp);
return ($output_file);
}
//随机数
public function getRandomString($len, $chars = null)
{
if (is_null($chars)) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
}
mt_srand(10000000 * (double)microtime());
for ($i = 0, $str = '', $lc = strlen($chars) - 1; $i < $len; $i++) {
$str .= $chars[mt_rand(0, $lc)];
}
return $str;
}
}
异步上传图片
最新推荐文章于 2022-08-24 18:07:45 发布