开发文档 v2.x

文件操作

更新时间:2024年1月9日 18:52 浏览:461

 

getFiles - 获取指定路径下的文件列表

public function getFiles(string $dirPath, array $option = []): array

参数:

  • string $dirPath - 文件夹路径 以 '/' 开头,以 '/' 结尾
  • array $option 参数

返回值:

  • array - 文件列表

 

代码示例:

$files = Be::getStorage()->getFiles('/');
/*
示例:
[
  [
    'name' => 'abc',
    'type' => 'dir',
    'size' => 0,
    'sizeString' => '0 B',
    'url' => 'https://cdn.phpbe.com/abc',
    'createTime' => '2022-08-01 10:00:00',
    'updateTime' => '2022-08-01 10:00:00'
  ],
  [
    'name' => 'abc.txt',
    'type' => 'txt',
    'size' => 1024,
    'sizeString' => '1 KB',
    'url' => 'https://cdn.phpbe.com/abc.txt',
    'createTime' => '2022-08-01 10:00:00',
    'updateTime' => '2022-08-01 10:00:00'
  ],
  // ...
]
*/

// 仅显示图像文件
$images = Be::getStorage()->getFiles('/', ['filterImage' => 1]);

 

uploadFile - 文件 - 上传 用户提交的临时文件

public function uploadFile(string $path, string $tmpFile, bool $override = false, bool $existException = true): string

参数:

  • string $path - 文件存储路径,以 '/' 开头
  • string $tmpFile - 上传的临时文件名或指定的文件
  • bool $override - 是否醒盖同名文件
  • bool $existException - 不醒盖但同名文件但存在时是否抛出异常

返回值:

  • string - 上传成功的文件的网址

 

代码示例:

// 通过表单上传的文件
$file = Be::getRequest()->files('xxx');

$path = '/abc/' .  trim($file['name']);
$url = Be::getStorage()->uploadFile($path, $file['tmp_name'], true);
// 返回网址
// https://cdn.phpbe.com/abc/xxx.jpg


// 上传已在服务器上的指定的文件到存储系统
$srcPath = Be::getRuntime()->getRootPath() . '/data/tmp/xxx.jpg';
$dstPath = '/abc/xxx.jpg';
$url = Be::getStorage()->uploadFile($dstPath, $srcPath, true);

 

renameFile - 文件 - 重命名

 public function renameFile(string $oldPath, string $newPath): string

参数:

  • string $oldPath - 旧文件路径 以 '/' 开头
  • string $newPath - 新文件路径 以 '/' 开头

返回值:

  • string - 重命名成功的新文件的网址

 

操作失败,旧文件不存在,新文件已存在 等情况会抛出异常

 

代码示例:

$srcPath = '/abc/xx.jpg';
$dstPath = '/def/xxx.jpg';
$url = Be::getStorage()->renameFile($srcPath, $dstPath);

 

deleteFile - 删除文件

public function deleteFile(string $path): bool

参数:

  • string $path - 文件存储路径,以 '/' 开头

返回值:

  • bool - 是否删除成功

 

代码示例:

$path = '/abc/xxx.jpg';
Be::getStorage()->deleteFile($path);

 

isFileExist - 文件是否存在

public function isFileExist(string $path): bool

参数:

  • string $path - 文件存储路径,以 '/' 开头

返回值:

  • bool - 是否存在

 

代码示例:

$path = '/abc/xxx.jpg';
if (Be::getStorage()->isFileExist($path)) {
  // ...
}

 

getRootUrl - 获取根网址

public function getRootUrl(): string

参数:

返回值:

  • string - 根网址

 

根网址即访问存储系统的根网址。

  • 当使用本地磁盘时,我们通常部署 CDN 加速,并配置一个独立的 CDN 专用域名,当然不使用 CDN 时,也可以用原始域名。指向 /www/ 即可。
  • 当使用云端的对象存储时,云平台会提供一个 对象存储 的域名。在对象存储 域名之上,通常也需要再部署一层 CDN 加速及 CDN 专用域名。

 

代码示例:

$storageRootUrl = Be::getStorage()->getRootUrl();
// https://cdn.phpbe.com

 

getFileUrl - 获取文件网址

public function getFileUrl(string $path): string

参数:

  • string $path - 文件存储路径,以 '/' 开头

返回值:

  • string - 文件网址

 

代码示例:

$path = '/abc/xxx.jpg';
$fileUrl = Be::getStorage()->getFileUrl($path);
// https://cdn.phpbe.com/abc/xxx.jpg

    

 

导航