Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
tl_estate
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
hujun
tl_estate
Commits
76da2f2b
Commit
76da2f2b
authored
Jul 03, 2018
by
clone
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
图片上传
parent
1fd1f103
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
73 additions
and
18 deletions
+73
-18
UploadImg.php
application/api_broker/controller/UploadImg.php
+46
-0
UploadFileService.php
application/api_broker/service/UploadFileService.php
+22
-18
route.php
application/route.php
+5
-0
No files found.
application/api_broker/controller/UploadImg.php
0 → 100644
View file @
76da2f2b
<?php
namespace
app\api_broker\controller
;
use
app\api_broker\extend\Basic
;
use
app\api_broker\service\UploadFileService
;
use
think\Request
;
/**
* Created by PhpStorm.
* User : zw
* Date : 2018/7/3
* Time : 10:44
* Intro:
*/
class
UploadImg
extends
Basic
{
private
$uFService
;
public
function
__construct
(
$request
=
null
)
{
parent
::
__construct
(
$request
);
$this
->
uFService
=
new
UploadFileService
();
}
/**
* 图片上传
* @return \think\Response
*/
public
function
uploadImg
()
{
header
(
'Access-Control-Allow-Origin:*'
);
set_time_limit
(
0
);
$file
=
request
()
->
file
(
'image'
);
$type
=
request
()
->
param
(
'type'
);
$uploadResult
=
$this
->
uFService
->
upload
(
$file
,
$type
);
if
(
$uploadResult
[
"code"
]
==
200
)
{
return
$this
->
response
(
"200"
,
"图片上传成功"
,
$uploadResult
[
"msg"
]);
}
else
{
return
$this
->
response
(
"101"
,
$uploadResult
[
"msg"
]);
}
}
}
\ No newline at end of file
application/api_broker/service/UploadFileService.php
View file @
76da2f2b
<?php
namespace
app\api_broker\service
;
/**
* Created by PhpStorm.
* User: hu jun
...
...
@@ -6,9 +7,6 @@
* Time: 16:21
*/
namespace
app\api_broker\service
;
use
think\File
;
class
UploadFileService
...
...
@@ -22,7 +20,7 @@ class UploadFileService
* @param array $ext
* @return array
*/
public
function
upload
(
$_upload_file
,
$type
,
$size
=
1000000
,
$ext
=
[
'jpg'
])
public
function
upload
(
$_upload_file
,
$type
,
$size
=
1000000
,
$ext
=
[
'jpg'
,
'png'
,
'jpeg'
])
{
/**
*
...
...
@@ -56,6 +54,9 @@ class UploadFileService
$path
=
ROOT_PATH
.
'public/'
;
switch
(
$type
)
{
case
'chat'
:
$path
.=
'static/chat_image/'
;
break
;
case
'user_header'
:
$path
.=
'static/user_header/'
;
break
;
...
...
@@ -66,7 +67,9 @@ class UploadFileService
$path
.=
'resource/lib/Attachments/images/'
;
break
;
default
:
$path
.=
'static/'
;
$data
[
'code'
]
=
101
;
$data
[
'msg'
]
=
"上传图片类型错误"
;
return
$data
;
}
$path
.=
date
(
'Ymd'
);
...
...
@@ -80,12 +83,13 @@ class UploadFileService
$info
=
$_file
->
validate
(
$valid_date
)
->
move
(
$path
,
$name_str
.
'.'
.
$file_info
[
'extension'
]);
if
(
$info
)
{
$data
[
$k
][
'code'
]
=
200
;
$data
[
$k
][
'img_path'
]
=
$info
->
getSaveName
();
//生成的图片路径
$data
[
$k
][
'img_ext'
]
=
$info
->
getExtension
();
$data
[
'code'
]
=
200
;
$data
[
"msg"
][
$k
][
'img_path'
]
=
$info
->
getSaveName
();
//生成的图片路径
$data
[
"msg"
][
$k
][
'img_ext'
]
=
$info
->
getExtension
();
}
else
{
$data
[
$k
][
'code'
]
=
101
;
$data
[
$k
][
'error'
]
=
$_file
->
getError
();
$data
[
'code'
]
=
101
;
$data
[
"msg"
][
$k
][
'error'
]
=
$_file
->
getError
();
}
}
...
...
@@ -96,11 +100,11 @@ class UploadFileService
$info
=
$_file
->
validate
(
$valid_date
)
->
move
(
$path
,
$name_str
.
'.'
.
$file_info
[
'extension'
]);
if
(
$info
)
{
$data
[
'code'
]
=
200
;
$data
[
'img_path'
]
=
$info
->
getSaveName
();
//生成的图片路径
$data
[
'img_ext'
]
=
$info
->
getExtension
();
$data
[
"msg"
][
'img_path'
]
=
$info
->
getSaveName
();
//生成的图片路径
$data
[
"msg"
][
'img_ext'
]
=
$info
->
getExtension
();
}
else
{
$data
[
'code'
]
=
101
;
$data
[
'
error
'
]
=
$_file
->
getError
();
$data
[
'
msg
'
]
=
$_file
->
getError
();
}
}
...
...
@@ -116,7 +120,7 @@ class UploadFileService
* @param $ext
* @return array
*/
p
ublic
function
checkUploadFile
(
$_upload_file
,
$size
,
$ext
)
p
rivate
function
checkUploadFile
(
$_upload_file
,
$size
,
$ext
)
{
$data
=
[];
if
(
is_array
(
$_upload_file
[
'tmp_name'
]))
{
...
...
@@ -126,13 +130,13 @@ class UploadFileService
if
(
filesize
(
$v
)
>
$size
)
{
$data
[
'code'
]
=
101
;
$data
[
'
error
'
]
=
'文件太大无法上传'
;
$data
[
'
msg
'
]
=
'文件太大无法上传'
;
break
;
}
if
(
!
in_array
(
$file_info
[
'extension'
],
$ext
))
{
$data
[
'code'
]
=
101
;
$data
[
'
error
'
]
=
'文件类型不符无法上传'
;
$data
[
'
msg
'
]
=
'文件类型不符无法上传'
;
break
;
}
...
...
@@ -141,13 +145,13 @@ class UploadFileService
if
(
filesize
(
$_upload_file
[
'tmp_name'
])
>
$size
)
{
$data
[
'code'
]
=
101
;
$data
[
'
error
'
]
=
'文件太大无法上传'
;
$data
[
'
msg
'
]
=
'文件太大无法上传'
;
}
$file_info
=
pathinfo
(
$_upload_file
[
'name'
]);
if
(
!
in_array
(
$file_info
[
'extension'
],
$ext
))
{
$data
[
'code'
]
=
101
;
$data
[
'
error
'
]
=
'文件类型不符无法上传'
;
$data
[
'
msg
'
]
=
'文件类型不符无法上传'
;
}
}
...
...
application/route.php
View file @
76da2f2b
...
...
@@ -485,6 +485,10 @@ Route::group('broker', [
'superviseUploadImg'
=>
[
'api_broker/Supervise/superviseUploadImg'
,
[
'method'
=>
'POST|GET'
]
],
//监督执行-上传图片 朱伟 2018-06-20
'uploadImg'
=>
[
'api_broker/UploadImg/uploadImg'
,
[
'method'
=>
'POST|GET'
]
],
//图片上传
]);
//Route::miss('api/index/miss');//处理错误的url
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment