sauvank/php-file-tools

文件处理简单工具

3.1.2 2020-10-20 13:26 UTC

This package is auto-updated.

Last update: 2024-09-20 22:18:03 UTC


README

一个简单的包,用于获取带有扩展名的文件并进行移动。

要求

  • PHP >=7.4

使用Docker运行(分支:docker)

./helper install

运行单元测试

  • 在文件夹app中

    vendor/bin/phpunit tests

    ./run_test.sh

  • 使用composer安装

    composer require sauvank/file-tools

功能列表

MoveFile::move(string $src, string $output, bool $createOutputPath [default : true], $windowsNameValid [default : true]): File

* string $src, source of the file to move
* string $output, output of the file
* bool $createOutputPath, default true, if the folder does not exist, create them.

Return instance of File in case of success or Exeption if error
.
  • $files, 多维数组。
    • 每个数组需要
      • 字符串 'src', 要移动的文件源
      • 字符串 'output', 文件的输出
    • 可选
      • bool 'createOutputPath' 默认true,如果文件夹不存在,则创建它们。
      • bool 'windowsNameValid' 默认true,修改输出路径以使其适用于Windows。
MoveFile::moveMultiple(array $files)

  • $files, 多维数组
    • 每个数组需要
      • 字符串 'src', 要移动的文件源
      • 输出 'src', 文件的输出
    • 可选
      • bool 'createOutputPath' 默认true,如果文件夹不存在,则创建它们。

在成功的情况下返回File实例数组或错误时返回异常

GetFile::byExtension(string $folderPath, array $extsToGet = [], array $excludeFolder = ['\$RECYCLE\.BIN', 'Trash-1000', 'found\.000'])

  • string $folderPath, 获取文件的文件夹路径
  • array $extsToGet, 包含要获取的扩展名文件的数组。例如:['mkv', 'mp4']
  • array $excludeFolder, 包含要排除的文件夹名称的数组。

返回File实例数组或错误时返回异常

GetFile::byFolderAndExtension(string $folderPath, array $extsToGet = [], array $excludeFolder = ['\$RECYCLE\.BIN', 'Trash-1000', 'found\.000'])

  • string $folderPath, 获取文件的文件夹路径
  • array $extsToGet, 包含要获取的扩展名文件的数组。例如:['mkv', 'mp4']
  • array $excludeFolder, 包含要排除的文件夹名称的数组。

返回以文件夹为键、以文件为值的多维数组,或错误时返回异常

示例返回:

["folder_path/folder 1"]=> [
    0 => object(FileTools\File),
    1 => object(FileTools\File),
    2 => object(FileTools\File),
],
["folder_path/folder 2"]=> [
        0 => object(FileTools\File),
        1 => object(FileTools\File),
        2 => object(FileTools\File),
],  

]

示例

移动一个文件

use FileTools\MoveFile;
$moveFile = new MoveFile();

try{
    $result = $mv->move('tests/test_unit.mp4', 'tests/test_unit.mp4');
    // instance of File
}catch (Exception $e){
    var_dump('Catch: ' . $e->getMessage());
}

移动多个文件

示例

use FileTools\MoveFile;
$moveFile = new MoveFile();
$data = [
    [
        'src' => 'tests/samples/fake_mkv.mkv',
        'output' => 'tests/samples/',
        'createOutputPath' => false
    ],
    [
        'src' => 'tests/samples/ii.mkv',
        'output' => 'tests/samples/',
        'createOutputPath' => false
    ],
    [
        'src' => 'tests/samples/fake_mkv.mkv',
        'output' => 'tests/samples/ii/',
        'createOutputPath' => false
    ],
];

try{
    $result = $moveFile->moveMultiple($data);
    // array instance of File
}catch (Exception $e){
    var_dump('Catch: ' . $e->getMessage());
}

File类函数

getDirname(): ?string
setDirname($dirname): void
getBasename(): ?string
setBasename($basename): void
getExtension(): ?string
setExtension($extension): void
getFilename(): ?string
setFilename($filename): void
getFullPath(): ?string
getMimeType(): ?string
getFileSize():?int
getLastPath(): ?string
setLastPath($lastPath): void