Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
meibuyu-rpc
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
without authentication
meibuyu-rpc
Commits
b9a8d496
Commit
b9a8d496
authored
Apr 27, 2020
by
王源
🎧
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修复上传路径中地址不全的bug
parent
ea523045
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
9 deletions
+21
-9
UploadManager.php
src/Manager/UploadManager.php
+21
-9
No files found.
src/Manager/UploadManager.php
View file @
b9a8d496
...
@@ -16,7 +16,7 @@ use Meibuyu\Micro\Exceptions\HttpResponseException;
...
@@ -16,7 +16,7 @@ use Meibuyu\Micro\Exceptions\HttpResponseException;
class
UploadManager
class
UploadManager
{
{
public
static
$pathPrefix
=
'
public
/upload/'
;
public
static
$pathPrefix
=
'/upload/'
;
public
static
$options
=
[
public
static
$options
=
[
'path'
=>
'default'
,
// 默认保存路径
'path'
=>
'default'
,
// 默认保存路径
'maxSize'
=>
100000000
,
// 文件大小
'maxSize'
=>
100000000
,
// 文件大小
...
@@ -67,6 +67,10 @@ class UploadManager
...
@@ -67,6 +67,10 @@ class UploadManager
*/
*/
public
static
function
uploadFile
(
$file
,
$options
=
[])
public
static
function
uploadFile
(
$file
,
$options
=
[])
{
{
$documentRoot
=
config
(
'server.settings.document_root'
);
if
(
!
$documentRoot
)
{
throw
new
\RuntimeException
(
'未配置静态资源'
);
}
$options
=
self
::
parseOptions
(
$options
);
$options
=
self
::
parseOptions
(
$options
);
if
(
$file
->
isValid
())
{
if
(
$file
->
isValid
())
{
$extension
=
$file
->
getExtension
();
$extension
=
$file
->
getExtension
();
...
@@ -80,10 +84,10 @@ class UploadManager
...
@@ -80,10 +84,10 @@ class UploadManager
}
}
// 文件重命名,由当前日期时间 + 唯一ID + 扩展名
// 文件重命名,由当前日期时间 + 唯一ID + 扩展名
$fileName
=
date
(
'YmdHis'
)
.
uniqid
()
.
'.'
.
$extension
;
$fileName
=
date
(
'YmdHis'
)
.
uniqid
()
.
'.'
.
$extension
;
$savePath
=
self
::
parsePath
(
$options
)
.
$fileName
;
$savePath
=
self
::
parsePath
(
$options
,
$documentRoot
)
.
$fileName
;
$file
->
moveTo
(
$savePath
);
$file
->
moveTo
(
$savePath
);
if
(
$file
->
isMoved
())
{
if
(
$file
->
isMoved
())
{
return
str_replace
(
'public/'
,
''
,
$savePath
);
return
str_replace
(
$documentRoot
,
''
,
$savePath
);
}
else
{
}
else
{
throw
new
HttpResponseException
(
'文件保存失败'
);
throw
new
HttpResponseException
(
'文件保存失败'
);
}
}
...
@@ -98,6 +102,10 @@ class UploadManager
...
@@ -98,6 +102,10 @@ class UploadManager
*/
*/
public
static
function
createAvatar
()
public
static
function
createAvatar
()
{
{
$documentRoot
=
config
(
'server.settings.document_root'
);
if
(
!
$documentRoot
)
{
throw
new
\RuntimeException
(
'未配置静态资源'
);
}
$img
=
imagecreatetruecolor
(
180
,
180
);
$img
=
imagecreatetruecolor
(
180
,
180
);
$bgColor
=
imagecolorallocate
(
$img
,
240
,
240
,
240
);
$bgColor
=
imagecolorallocate
(
$img
,
240
,
240
,
240
);
imagefill
(
$img
,
0
,
0
,
$bgColor
);
imagefill
(
$img
,
0
,
0
,
$bgColor
);
...
@@ -124,28 +132,29 @@ class UploadManager
...
@@ -124,28 +132,29 @@ class UploadManager
$i
+=
14
;
$i
+=
14
;
}
}
$path
=
self
::
$pathPrefix
.
'avatar/default/'
;
$path
=
$documentRoot
.
self
::
$pathPrefix
.
'avatar/default/'
;
if
(
!
is_dir
(
$path
))
{
if
(
!
is_dir
(
$path
))
{
mkdir
(
$path
,
0777
,
true
);
mkdir
(
$path
,
0777
,
true
);
}
}
$fileName
=
$path
.
date
(
'YmdHis'
)
.
uniqid
()
.
'.png'
;
$fileName
=
$path
.
date
(
'YmdHis'
)
.
uniqid
()
.
'.png'
;
imagepng
(
$img
,
$fileName
);
imagepng
(
$img
,
$fileName
);
imagedestroy
(
$img
);
//释放内存
imagedestroy
(
$img
);
//释放内存
return
str_replace
(
'public/'
,
''
,
$fileName
);
return
str_replace
(
$documentRoot
,
''
,
$fileName
);
}
}
/**
/**
* 处理保存路径
* 处理保存路径
* @param $options
* @param $options
* @param $documentRoot
* @return string
* @return string
*/
*/
public
static
function
parsePath
(
$options
)
public
static
function
parsePath
(
$options
,
$documentRoot
)
{
{
if
(
isset
(
$options
[
'temp'
])
&&
$options
[
'temp'
]
&&
!
isset
(
$options
[
'path'
]))
{
if
(
isset
(
$options
[
'temp'
])
&&
$options
[
'temp'
]
&&
!
isset
(
$options
[
'path'
]))
{
// 如果是临时文件,且没有指定保存路径,修改保存路径为临时路径
// 如果是临时文件,且没有指定保存路径,修改保存路径为临时路径
$options
[
'path'
]
=
'temp'
;
$options
[
'path'
]
=
'temp'
;
}
}
$path
=
self
::
$pathPrefix
.
$options
[
'path'
]
.
'/'
.
date
(
'Y-m-d'
);
$path
=
$documentRoot
.
self
::
$pathPrefix
.
$options
[
'path'
]
.
'/'
.
date
(
'Y-m-d'
);
if
(
!
is_dir
(
$path
))
{
if
(
!
is_dir
(
$path
))
{
// 判断路径是否存在,不存在,则创建
// 判断路径是否存在,不存在,则创建
mkdir
(
$path
,
0777
,
true
);
mkdir
(
$path
,
0777
,
true
);
...
@@ -169,9 +178,12 @@ class UploadManager
...
@@ -169,9 +178,12 @@ class UploadManager
public
static
function
deleteFile
(
$path
)
public
static
function
deleteFile
(
$path
)
{
{
$documentRoot
=
config
(
'server.settings.document_root'
);
if
(
!
$documentRoot
)
{
throw
new
\RuntimeException
(
'未配置静态资源'
);
}
$path
=
str_replace
(
config
(
'app_domain'
),
''
,
$path
);
$path
=
str_replace
(
config
(
'app_domain'
),
''
,
$path
);
$path
=
ltrim
(
$path
,
'/'
);
$path
=
$documentRoot
.
$path
;
$path
=
'public/'
.
$path
;
if
(
file_exists
(
$path
))
{
if
(
file_exists
(
$path
))
{
unlink
(
$path
);
unlink
(
$path
);
}
}
...
...
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