Files
code/application/adminghd/controller/Files.php
2026-01-28 10:16:06 +08:00

119 lines
3.9 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
namespace app\adminghd\controller;
use app\adminghd\common\Base;
use think\Request;
//use think\Db;
//use think\Log;
//use think\Image;
use think\Response;
/**
*上传文件
*/
class Files extends Base
{
/**
*上传图片
*@param
*/
public function uploadImg(Request $request)
{
$typeArr = array("jpg","png",'jpeg');//图片类型
$path = "uploads/" . date('Y/m/d/');//上传路径
if(!file_exists($path)){
//创建文件
mkdir($path,0777,true);
}
if ($_FILES) {
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$name_tmp = $_FILES['file']['tmp_name'];
if (empty($name)) {
return json(array("status"=>"-1","error"=>"图片不能为空"));
exit;
}
$type = strtolower(substr(strrchr($name, '.'), 1)); //获取文件类型
if (!in_array($type, $typeArr)) {
return json(array("status"=>"-1","error"=>"请上传jpg,png,或jpeg类型的文件"));//mov,webm
exit;
}
if ($size > (2 * 1024 * 1024)) {
return json(array("status"=>"-1","error"=>"文件大小已超过2MB"));
exit;
}
/* if($_GET['type'] == 'cx'){
$imageType = 'cx';
}*/
$imageType='';
$pic_name = $imageType.time() . rand(10000, 99999) . "." . $type;//图片名称
$pic_url =$path . $pic_name;//上传后图片路径+名称
if (move_uploaded_file($name_tmp, $pic_url)) { //临时文件转移到目标文件夹
/* $_POST['name'] = $pic_name;
$_POST['topic'] = $imageType;
$_POST['albumPath'] = $pic_url;*/
return json(array("status"=>"0","file_name"=>$pic_name,"file_path"=>$request->domain().'/'.$pic_url));
die();
} else {
return json(array("status"=>"-1","error"=>"上传有误,请检查服务器配置!"));
die();
}
}else{
return json(array("status"=>"-1","error"=>"系统错误!"));
die();
}
}
/**
*上传视频
*/
public function uploadVideo(Request $request){
$typeArr = array("mp4","wkv");//官方推荐 mp4
$path = "uploadnv/" . date('Y/m/d/');//上传路径
if(!file_exists($path)){
//创建文件
mkdir($path,0777,true);
}
// if ($_FILES) {
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$name_tmp = $_FILES['file']['tmp_name'];
if (empty($name)) {
// return json_encode(['status'=>0,'msg'=>'查询失败']);
echo json_encode(array("error"=>"您还未选择文件"));
exit;
}
$type = strtolower(substr(strrchr($name, '.'), 1)); //获取文件类型
if (!in_array($type, $typeArr)) {
echo json_encode(array("error"=>"请上传mp4,wkv类型的文件"));//mov,webm
exit;
}
if ($size > (15 * 1024 * 1024)) {
echo json_encode(array("error"=>"文件大小已超过30MB"));
exit;
}
/* if($_GET['type'] == 'cx'){
$imageType = 'cx';
}*/
$imageType='';
$pic_name = $imageType.time() . rand(10000, 99999) . "." . $type;//图片名称
$pic_url =$path . $pic_name;//上传后图片路径+名称
if (move_uploaded_file($name_tmp, $pic_url)) { //临时文件转移到目标文件夹
/* $_POST['name'] = $pic_name;
$_POST['topic'] = $imageType;
$_POST['albumPath'] = $pic_url;*/
echo json_encode(array("status"=>"0","file_name"=>$pic_name,"file_path"=>$request->domain().'/'.$pic_url));
die();
} else {
echo json_encode(array("error"=>"上传有误,请检查服务器配置!"));
die();
}
//}
}
}