Skip to content

File — 文件操作

File 提供文件和目录操作的静态方法集合,包括文件上传、目录复制/删除、文件创建等。

  • 命名空间: kernel\Foundation
  • 文件位置: kernel/Foundation/File.php
  • 特点: 全部为静态方法

注意: 该类标记为 @deprecated,新代码建议使用 kernel\Foundation\File\FileHelperkernel\Foundation\File\FileManager

方法列表

isVideo($fileName)

判断指定文件是否为视频文件(文件必须存在)。

参数类型说明
$fileNamestring文件完整路径

返回值:bool

php
if (File::isVideo("/path/to/movie.mp4")) {
    // 是视频文件
}

isImage($fileName)

判断指定文件是否为图片文件(文件必须存在)。

参数类型说明
$fileNamestring文件完整路径

返回值:bool

php
if (File::isImage("/path/to/photo.png")) {
    // 是图片文件
}

upload($files, $savePath, $fileName = null)

上传文件并保存到服务器。

参数类型说明
$filesarray|string上传的文件或文件路径字符串
$savePathstring保存的目标目录完整路径
$fileNamestring|null保存的文件名(不含扩展名),null 自动生成

返回值:array — 文件信息数组,包含 pathextensionsourceFileNamesizefullPath

php
$result = File::upload($_FILES['avatar'], F_APP_STORAGE . "/avatars");
// ["path" => "...", "extension" => "png", "size" => 12345, "fullPath" => "..."]

cloneDirectory($sourcePath, $destPath)

克隆目录(递归复制所有文件和子目录)。

参数类型说明
$sourcePathstring源目录路径
$destPathstring目标目录路径
php
File::cloneDirectory("/path/source", "/path/dest");

createFile($filePath, $fileContent = "", $overwrite = false)

创建文件。

参数类型说明
$filePathstring文件完整路径
$fileContentstring文件内容
$overwritebool是否覆盖已存在的文件

返回值:bool

php
File::createFile("/path/to/file.txt", "Hello World");
File::createFile("/path/to/file.txt", "New Content", true);  // 覆盖

deleteDirectory($path)

递归删除目录及其下所有文件(不可恢复,谨慎使用)。

参数类型说明
$pathstring要删除的目录路径

返回值:bool

php
File::deleteDirectory("/path/to/temp");

mkdir($dirs, $baseDir = "", $permissions = 0757)

创建文件夹。

参数类型说明
$dirsarray路径项数组
$baseDirstring基目录
$permissionsint文件夹权限

返回值:bool

php
File::mkdir(["storage", "uploads"], F_APP_ROOT);
// 创建 {F_APP_ROOT}/storage/uploads

genPath(...$els)

生成路径字符串(自动使用系统分隔符)。

参数类型说明
$elsstring路径组成部分

返回值:string

php
$path = File::genPath(F_APP_ROOT, "Storage", "images");
// 在 Linux 上: /path/to/app/Storage/images

scandir($targetPath, $sorting_order = 0, $context = null)

扫描目录,排除 ...

参数类型说明
$targetPathstring目录路径
$sorting_orderint排序方式
$contextmixed上下文

返回值:array|false

php
$files = File::scandir("/path/to/dir");
// ["file1.txt", "file2.txt", "subdir"]

clearFolder($targetPath, $whiteList = [])

清空文件夹内容(保留文件夹本身)。

参数类型说明
$targetPathstring目标文件夹路径
$whiteListarray跳过的白名单路径

返回值:bool

php
File::clearFolder("/path/to/cache", ["/path/to/cache/keep.txt"]);

copyFolder($targetPath, $destPath, $whiteList = [])

复制文件夹(递归复制)。

参数类型说明
$targetPathstring源目录
$destPathstring目标目录
$whiteListarray跳过的白名单路径

返回值:bool

compareDirectories($targetPath, $sourcePath)

比较两个目录是否相等(深度比较)。

参数类型说明
$targetPathstring目录 1
$sourcePathstring目录 2

返回值:bool

recursionScanDir($rootDir, $includeDir = false)

递归扫描目录下的所有文件。

参数类型说明
$rootDirstring根目录
$includeDirbool是否包含目录路径

返回值:string[]

php
$allFiles = File::recursionScanDir("/path/to/project");
// ["/path/to/project/a.php", "/path/to/project/b.php", ...]

与其他类的协作

关系说明
App加载文件用于加载路由文件
Cache文件操作缓存底层使用文件存储