文件系统助手函数
Testing Is Documentation
Uses
php
<?php
use Leevel\Filesystem\Helper;
use Leevel\Kernel\Utils\Api;
use Symfony\Component\Filesystem\Exception\IOException;
create_directory 创建目录
php
public function testCreateDirectory(): void
{
$dir = __DIR__.'/createDirectory';
self::assertDirectoryDoesNotExist($dir);
Helper::createDirectory($dir);
self::assertDirectoryExists($dir);
Helper::createDirectory($dir);
Helper::createDirectory($dir);
Helper::deleteDirectory($dir);
}
delete_directory 删除目录
php
public function testDeleteDirectory(): void
{
$dir = __DIR__.'/deleteDirectory/dir';
self::assertDirectoryDoesNotExist($dir);
Helper::deleteDirectory($dir);
Helper::createDirectory($dir);
self::assertDirectoryExists($dir);
Helper::deleteDirectory($dir);
$topDir = \dirname($dir);
self::assertDirectoryExists($topDir);
Helper::deleteDirectory($topDir);
self::assertDirectoryDoesNotExist($topDir);
}
traverse_directory 遍历目录
php
public function testTraverseDirectory(): void
{
$sourcePath = __DIR__.'/traverseDirectory';
$sourceSubPath = __DIR__.'/traverseDirectory/dir';
self::assertDirectoryDoesNotExist($sourceSubPath);
Helper::createDirectory($sourceSubPath);
file_put_contents($testFile = $sourceSubPath.'/hello.txt', 'foo');
self::assertTrue(is_file($testFile));
self::assertSame('foo', file_get_contents($testFile));
$filesAndDirs = [];
$filesAndDirs2 = [];
Helper::traverseDirectory($sourcePath, true, static function ($item) use (&$filesAndDirs): void {
$filesAndDirs[] = $item->getFileName();
});
Helper::traverseDirectory($sourcePath, true, static function ($item) use (&$filesAndDirs2): void {
$filesAndDirs2[] = $item->getFileName();
}, ['hello.txt']);
self::assertSame(['dir', 'hello.txt'], $filesAndDirs);
self::assertSame(['dir'], $filesAndDirs2);
Helper::deleteDirectory($sourcePath);
}
tidy_path 整理目录斜线风格
php
public function testTidyPath(): void
{
$sourcePath = '/home\goods/name/';
self::assertSame('/home/goods/name', Helper::tidyPath($sourcePath));
self::assertSame('\home\goods\name', Helper::tidyPath($sourcePath, false));
}
is_absolute_path 判断是否为绝对路径
php
public function testIsAbsolutePath(): void
{
self::assertTrue(Helper::isAbsolutePath('c://'));
self::assertTrue(Helper::isAbsolutePath('/path/hello'));
self::assertFalse(Helper::isAbsolutePath('hello'));
}
distributed 根据 ID 获取打散目录
php
public function testDistributed(): void
{
self::assertSame(['000/00/00/', '01'], Helper::distributed(1));
self::assertSame(['090/00/00/', '00'], Helper::distributed(90000000));
}
create_file 创建文件
php
public function testCreateFile(): void
{
$sourcePath = __DIR__.'/createFile';
$file = $sourcePath.'/hello.txt';
self::assertDirectoryDoesNotExist($sourcePath);
Helper::createDirectory($sourcePath);
self::assertFalse(is_file($file));
Helper::createFile($file);
self::assertTrue(is_file($file));
Helper::deleteDirectory($sourcePath);
}
get_extension 获取上传文件扩展名
php
public function testGetExtension(): void
{
$file = __DIR__.'/HelperTest.pHp';
self::assertSame('pHp', Helper::getExtension($file));
self::assertSame('PHP', Helper::getExtension($file, 1));
self::assertSame('php', Helper::getExtension($file, 2));
}