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
18c016f3
Commit
18c016f3
authored
Jun 25, 2018
by
hujun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
UploadFileService 文件上传
parent
fc6a635c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
137 additions
and
0 deletions
+137
-0
UploadFileService.php
application/api_broker/service/UploadFileService.php
+137
-0
No files found.
application/api_broker/service/UploadFileService.php
0 → 100644
View file @
18c016f3
<?php
/**
* Created by PhpStorm.
* User: hu jun
* Date: 2018/6/22
* Time: 16:21
*/
namespace
app\api_broker\service
;
use
think\File
;
class
UploadFileService
{
/**
* 文件上传
*
* @param $_upload_file
* @param $type
* @param int $size
* @param array $ext
* @return array
*/
public
function
upload
(
$_upload_file
,
$type
,
$size
=
1000000
,
$ext
=
[
'jpg'
])
{
$check
=
$this
->
checkUploadFile
(
$_upload_file
,
$size
,
$ext
);
if
(
isset
(
$check
[
'code'
])
||
isset
(
$check
[
0
][
'code'
]))
{
return
$check
;
}
$data
=
[];
$valid_date
[
'size'
]
=
$size
;
$path
=
ROOT_PATH
.
'public/'
;
switch
(
$type
)
{
case
1
:
$path
.=
'static/user_header/'
;
break
;
case
2
:
$path
.=
'static/head_portrait/'
;
break
;
case
3
:
$path
.=
'resource/lib/Attachments/images/'
;
default
:
$path
.=
'static/'
;
}
$path
.=
date
(
'Ymd'
);
$name_str
=
date
(
'YmdHis'
)
.
mt_rand
(
1000
);
if
(
is_array
(
$_upload_file
[
'tmp_name'
]))
{
foreach
(
$_upload_file
[
'tmp_name'
]
as
$k
=>
$v
)
{
$name_str
.=
$k
;
$_file
=
new
File
(
$v
);
$file_info
=
pathinfo
(
$_upload_file
[
'name'
][
$k
]);
$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
();
}
else
{
$data
[
$k
][
'code'
]
=
101
;
$data
[
$k
][
'error'
]
=
$_file
->
getError
();
}
}
}
else
{
$_file
=
new
File
(
$_upload_file
[
'tmp_name'
]);
$file_info
=
pathinfo
(
$_upload_file
[
'name'
]);
$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
();
}
else
{
$data
[
'code'
]
=
101
;
$data
[
'error'
]
=
$_file
->
getError
();
}
}
return
$data
;
}
/**
* 检查文件大小事
*
* @param $_upload_file
* @param $size
* @param $ext
* @return array
*/
public
function
checkUploadFile
(
$_upload_file
,
$size
,
$ext
)
{
$data
=
[];
if
(
is_array
(
$_upload_file
[
'tmp_name'
]))
{
foreach
(
$_upload_file
[
'tmp_name'
]
as
$k
=>
$v
)
{
$file_info
=
pathinfo
(
$_upload_file
[
'name'
][
$k
]);
if
(
filesize
(
$v
)
>
$size
)
{
$data
[
0
][
'code'
]
=
101
;
$data
[
0
][
'error'
]
=
'文件太大无法上传'
;
break
;
}
if
(
!
in_array
(
$file_info
[
'extension'
],
$ext
))
{
$data
[
0
][
'code'
]
=
101
;
$data
[
0
][
'error'
]
=
'文件类型不符无法上传'
;
break
;
}
}
}
else
{
if
(
filesize
(
$_upload_file
[
'tmp_name'
])
>
$size
)
{
$data
[
'code'
]
=
101
;
$data
[
'error'
]
=
'文件太大无法上传'
;
}
$file_info
=
pathinfo
(
$_upload_file
[
'name'
]);
if
(
!
in_array
(
$file_info
[
'extension'
],
$ext
))
{
$data
[
'code'
]
=
101
;
$data
[
'error'
]
=
'文件类型不符无法上传'
;
}
}
return
$data
;
}
}
\ 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