Browse Source

image upload

master
林一峰 7 years ago
parent
commit
56fcda9ea8
  1. 130
      app/Http/Controllers/Api/ImageController.php
  2. 2
      app/Http/Controllers/Api/UserController.php
  3. 2
      app/Http/Logic/UserLogic.php
  4. 2
      app/Http/Requests/UserRequest.php
  5. 2
      resources/views/weixin/user/userinfo.blade.php

130
app/Http/Controllers/Api/ImageController.php

@ -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)

2
app/Http/Controllers/Api/UserController.php

@ -85,7 +85,7 @@ class UserController extends CommonController
return ReturnData::create(ReturnData::PARAMS_ERROR,null,'用户名已存在');
}
}
$data = [];
if($request->input('email', null)!==null){$data['email'] = $request->input('email');}
if($request->input('sex', null)!==null){$data['sex'] = $request->input('sex');}
if($request->input('birthday', null)!==null){$data['birthday'] = $request->input('birthday');}

2
app/Http/Logic/UserLogic.php

@ -104,6 +104,8 @@ class UserLogic extends BaseLogic
$validator = $this->getValidate($data, 'edit');
if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
$data['updated_at'] =time();
$res = $this->getModel()->edit($data,$where);
if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}

2
app/Http/Requests/UserRequest.php

@ -79,7 +79,7 @@ class UserRequest extends BaseRequest
//场景验证规则
protected $scene = [
'add' => ['email', 'user_name', 'password', 'pay_password', 'head_img', 'sex', 'birthday', 'commission', 'money', 'frozen_money', 'point', 'rank_points', 'address_id', 'add_time', 'user_rank', 'parent_id', 'nickname', 'mobile', 'status', 'group_id', 'updated_at', 'signin_time', 'openid', 'unionid', 'push_id', 'refund_account', 'refund_name', 'consumption_money'],
'edit' => ['email', 'user_name', 'password', 'pay_password', 'head_img', 'sex', 'birthday', 'commission', 'money', 'frozen_money', 'point', 'rank_points', 'address_id', 'add_time', 'user_rank', 'parent_id', 'nickname', 'mobile', 'status', 'group_id', 'updated_at', 'signin_time', 'openid', 'unionid', 'push_id', 'refund_account', 'refund_name', 'consumption_money'],
'edit' => ['email', 'head_img', 'sex', 'birthday', 'address_id', 'nickname', 'updated_at', 'refund_account', 'refund_name'],
'wx_register' => ['email', 'user_name', 'password', 'pay_password', 'head_img', 'add_time', 'parent_id', 'nickname', 'mobile', 'group_id', 'openid', 'unionid', 'push_id'],
'del' => ['id'],
];

2
resources/views/weixin/user/userinfo.blade.php

@ -43,7 +43,7 @@ $(function(){
$("#head_img").ajaxSubmit({
dataType: 'json',
success: function(res) {
var img = res.data;
var img = res.data[0];
if(res.code==0)
{
$("#avator").attr("src",img);

Loading…
Cancel
Save