2020-11-21 19:53:01 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Service;
|
|
|
|
|
|
|
|
use Hyperf\HttpMessage\Upload\UploadedFile;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 文件上传服务
|
|
|
|
*
|
|
|
|
* @package App\Service
|
|
|
|
*/
|
|
|
|
class UploadService extends BaseService
|
|
|
|
{
|
|
|
|
public function driver($dir)
|
|
|
|
{
|
|
|
|
return sprintf('%s/%s', rtrim(config('upload_dir'), '/'), trim($dir, '/'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 创建文件夹
|
|
|
|
*
|
2020-12-02 15:14:29 +08:00
|
|
|
* @param string $dir 文件夹路径
|
2020-11-21 19:53:01 +08:00
|
|
|
*/
|
2021-04-22 16:14:34 +08:00
|
|
|
public function makeDirectory(string $dir)
|
2020-11-21 19:53:01 +08:00
|
|
|
{
|
|
|
|
if (!file_exists($dir)) @mkdir($dir, 0777, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 上传媒体图片
|
|
|
|
*
|
|
|
|
* @param UploadedFile $file
|
2021-04-20 16:30:57 +08:00
|
|
|
* @param string $dir 文件夹路径
|
|
|
|
* @param string $filename 文件名称
|
2020-12-02 15:14:29 +08:00
|
|
|
* @return bool|string
|
2020-11-21 19:53:01 +08:00
|
|
|
*/
|
|
|
|
public function media(UploadedFile $file, string $dir, string $filename)
|
|
|
|
{
|
|
|
|
$save_dir = $this->driver($dir);
|
2020-12-02 15:14:29 +08:00
|
|
|
|
2020-11-27 19:48:41 +08:00
|
|
|
$this->makeDirectory($save_dir);
|
2020-11-21 19:53:01 +08:00
|
|
|
|
|
|
|
$file->moveTo(sprintf('%s/%s', $save_dir, $filename));
|
2020-11-28 19:59:10 +08:00
|
|
|
|
|
|
|
if ($file->isMoved()) {
|
|
|
|
@chmod(sprintf('%s/%s', $save_dir, $filename), 0644);
|
|
|
|
}
|
|
|
|
|
2020-12-12 23:36:20 +08:00
|
|
|
return $file->isMoved() ? sprintf('%s/%s', trim($dir, '/'), $filename) : false;
|
2020-11-21 19:53:01 +08:00
|
|
|
}
|
|
|
|
}
|