|
|
@ -16,57 +16,6 @@ class ImageController extends CommonController |
|
|
|
$this->path = '/uploads/'.date('Y/m',time()); |
|
|
|
} |
|
|
|
|
|
|
|
//单文件/图片上传,成功返回路径,不含域名
|
|
|
|
public function imageUpload(Request $request) |
|
|
|
{ |
|
|
|
$file = $_FILES['file'];//得到传输的数据
|
|
|
|
|
|
|
|
$type = strtolower(substr(strrchr($file["name"], '.'), 1)); //文件后缀
|
|
|
|
|
|
|
|
$image_path = $this->path.'/'.date('Ymdhis',time()).rand(1000,9999).'.'.$type; |
|
|
|
$uploads_path = $this->path; //存储路径
|
|
|
|
|
|
|
|
$allow_type = array('jpg','jpeg','gif','png','doc','docx','txt','pdf'); //定义允许上传的类型
|
|
|
|
|
|
|
|
//判断文件类型是否被允许上传
|
|
|
|
if(!in_array($type, $allow_type)) |
|
|
|
{ |
|
|
|
//如果不被允许,则直接停止程序运行
|
|
|
|
return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'文件格式不正确'); |
|
|
|
} |
|
|
|
|
|
|
|
//判断是否是通过HTTP POST上传的
|
|
|
|
if(!is_uploaded_file($file['tmp_name'])) |
|
|
|
{ |
|
|
|
//如果不是通过HTTP POST上传的
|
|
|
|
return ReturnData::create(ReturnData::SYSTEM_FAIL); |
|
|
|
} |
|
|
|
|
|
|
|
//文件小于1M
|
|
|
|
if ($file["size"] < 2048000) |
|
|
|
{ |
|
|
|
if ($file["error"] > 0) |
|
|
|
{ |
|
|
|
return ReturnData::create(ReturnData::SYSTEM_FAIL,null,$file["error"]); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
if(!file_exists(base_path('public').$uploads_path)) |
|
|
|
{ |
|
|
|
Helper::createDir(base_path('public').$uploads_path); //创建文件夹;
|
|
|
|
} |
|
|
|
|
|
|
|
move_uploaded_file($file["tmp_name"], base_path('public').$image_path); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'文件不得超过2M'); |
|
|
|
} |
|
|
|
|
|
|
|
return ReturnData::create(ReturnData::SUCCESS,$image_path); |
|
|
|
} |
|
|
|
|
|
|
|
//阿里云OSS图片上传
|
|
|
|
public function ossImageUpload() |
|
|
|
{ |
|
|
@ -82,84 +31,25 @@ class ImageController extends CommonController |
|
|
|
|
|
|
|
/** |
|
|
|
* 多文件上传,成功返回路径,不含域名 |
|
|
|
* 多文件上传格式: |
|
|
|
* <input type="file" name="file[]"> |
|
|
|
* <input type="file" name="file[]"> |
|
|
|
* <input type="file" name="file[]"> |
|
|
|
*/ |
|
|
|
public function multipleImageUpload(Request $request) |
|
|
|
{ |
|
|
|
$res = []; |
|
|
|
$file = $_FILES['file'];//得到传输的数据
|
|
|
|
|
|
|
|
if($file) |
|
|
|
{ |
|
|
|
foreach($file['name'] as $key=>$value) |
|
|
|
{ |
|
|
|
$type = strtolower(substr(strrchr($file["name"][$key], '.'), 1)); //文件后缀
|
|
|
|
|
|
|
|
$image_path = $this->path.'/'.date('Ymdhis',time()).rand(1000,9999).'.'.$type; |
|
|
|
$uploads_path = $this->path; //存储路径
|
|
|
|
|
|
|
|
$allow_type = array('jpg','jpeg','gif','png','doc','docx','txt','pdf'); //定义允许上传的类型
|
|
|
|
|
|
|
|
//判断文件类型是否被允许上传
|
|
|
|
if(!in_array($type, $allow_type)) |
|
|
|
{ |
|
|
|
//如果不被允许,则直接停止程序运行
|
|
|
|
return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'文件格式不正确'); |
|
|
|
} |
|
|
|
|
|
|
|
//判断是否是通过HTTP POST上传的
|
|
|
|
if(!is_uploaded_file($file['tmp_name'][$key])) |
|
|
|
{ |
|
|
|
//如果不是通过HTTP POST上传的
|
|
|
|
return ReturnData::create(ReturnData::SYSTEM_FAIL); |
|
|
|
} |
|
|
|
|
|
|
|
//文件小于2M
|
|
|
|
if ($file["size"][$key] < 2048000) |
|
|
|
{ |
|
|
|
if ($file["error"][$key] > 0) |
|
|
|
{ |
|
|
|
return ReturnData::create(ReturnData::SYSTEM_FAIL,null,$file["error"][$key]); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
if(!file_exists(base_path('public').$uploads_path)) |
|
|
|
{ |
|
|
|
Helper::createDir(base_path('public').$uploads_path); //创建文件夹;
|
|
|
|
} |
|
|
|
|
|
|
|
move_uploaded_file($file["tmp_name"][$key], base_path('public').$image_path); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'文件不得超过2M'); |
|
|
|
} |
|
|
|
|
|
|
|
$res[] = $image_path; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return ReturnData::create(ReturnData::SUCCESS,$res); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 多文件上传,成功返回路径,不含域名 |
|
|
|
* 多文件上传格式: |
|
|
|
* 格式1: |
|
|
|
* <input type="file" name="file1"> |
|
|
|
* <input type="file" name="file2"> |
|
|
|
* <input type="file" name="file3"> |
|
|
|
* 格式2: |
|
|
|
* <input type="file" name="file[]"> |
|
|
|
* <input type="file" name="file[]"> |
|
|
|
* <input type="file" name="file[]"> |
|
|
|
*/ |
|
|
|
public function multipleFileUpload(Request $request) |
|
|
|
public function imageUpload(Request $request) |
|
|
|
{ |
|
|
|
$res = []; |
|
|
|
$files = $_FILES;//得到传输的数据
|
|
|
|
|
|
|
|
if($files) |
|
|
|
{ |
|
|
|
// 对上传文件数组信息处理
|
|
|
|
$files = $this->dealFiles($files); |
|
|
|
|
|
|
|
foreach($files as $key=>$file) |
|
|
|
{ |
|
|
|
$type = strtolower(substr(strrchr($file["name"], '.'), 1)); //文件后缀
|
|
|
@ -291,7 +181,7 @@ class ImageController extends CommonController |
|
|
|
/** |
|
|
|
* 转换上传文件数组变量为正确的方式 |
|
|
|
* @access public |
|
|
|
* @param array $files 上传的文件变量 |
|
|
|
* @param array $files 上传的文件变量 |
|
|
|
* @return array |
|
|
|
*/ |
|
|
|
public function dealFiles($files) |
|
|
|