Files
Introduction
Files
provides basic utilities for the filesystem.
Installation
composer require aphp/files ~1.1.0
Syntax
Init
$path = new FilePath('C:\aphp_git\projectGenerator\hello.jpg');
Path Interface
interface PathInterface
{
function replacePath($path, $rightIndex = 1);
// string
function getPath($index = -1);
function pathLength(); // int
function add($path); // $this FilePath
function pop($count = 1); // $this FilePath
}
getPath
$e[] = $path->getPath(2);
$e[] = $path->getPath(1);
$e[] = $path->getPath(0);
$e[] = $path->getPath(-1);
$e[] = $path->getPath(-2);
/*
[2] => projectGenerator/hello.jpg
[1] => aphp_git/projectGenerator/hello.jpg
[0] => C:/aphp_git/projectGenerator/hello.jpg
[-1] => C:/aphp_git/projectGenerator
[-2] => C:/aphp_git
*/
Table of path index (0, 1, 2, 3, 4, 5, 6).
$index values:
- positive index, sets offset at start.
- 3 => (3, 4, 5, 6)
- 2 => (2, 3, 4, 5, 6)
- 1 => (1, 2, 3, 4, 5, 6)
- zero index returns full path
- 0 => (0, 1, 2, 3, 4, 5, 6)
- negative index, sets offset at end.
- -1 => (0, 1, 2, 3, 4, 5)
- -2 => (0, 1, 2, 3, 4)
- -3 => (0, 1, 2, 3)
path length
Return a length of a path array.
add pop
$e[] = $path->getPath(0);
$path->pop();
$e[] = $path->getPath(0);
$path->add('image01.jpg');
$e[] = $path->getPath(0);
$path->pop()->add('image02.jpg');
$e[] = $path->getPath(0);
print_r($e);
/*
[0] => C:/aphp_git/projectGenerator/hello.jpg
[1] => C:/aphp_git/projectGenerator
[2] => C:/aphp_git/projectGenerator/image01.jpg
[3] => C:/aphp_git/projectGenerator/image02.jpg
*/
replacePath
$orig = $path->getPath();
$path->replacePath('X:/tests/folder', 0);
$e[] = $path->getPath(0);
$path->replacePath($orig, 0);
$path->replacePath('X:/tests/folder', 1);
$e[] = $path->getPath(0);
$path->replacePath($orig, 0);
$path->replacePath('X:/tests/folder', 2);
$e[] = $path->getPath(0);
echo $orig . PHP_EOL;
print_r($e);
/*
C:/aphp_git/projectGenerator/otherName.exe
Array
(
[0] => X:/tests/folder
[1] => X:/tests/folder/otherName.exe
[2] => X:/tests/folder/projectGenerator/otherName.exe
)*/
File Interface
interface FileInterface
{
// string
function fileName($extension = true);
function extension();
function replaceFileName($filename, $extension = true);
function replaceExtension($extension);
function getContents();
function putContents($text);
}
replaceFileName, replaceExtension
$e[] = $path->getPath();
$path->replaceFileName('newName.txt');
$e[] = $path->getPath();
$path->replaceFileName('otherName', false);
$e[] = $path->getPath();
$path->replaceExtension('exe');
$e[] = $path->getPath();
print_r($e);
/*
[0] => C:/aphp_git/projectGenerator/image02.jpg
[1] => C:/aphp_git/projectGenerator/newName.txt
[2] => C:/aphp_git/projectGenerator/otherName.txt
[3] => C:/aphp_git/projectGenerator/otherName.exe
*/
File System Interface
interface FileSystemInterface
{
// r = recursive
// f = files
// d = dir
// o = return objects
// s = sort by name (natural)
// n = native return SplFileInfo
// h = hidden files (ignoreVCS)
function getPathListOpt($flags = 'rfdosh', /* function(Finder $finder){ } */ $finderConfigFn = null);
function isRoot($_win = null);
function isExists();
function mkdir();
/**
* @param function(Finder $finder) $finderConfigFn
* @param SplFileInfo|string $destination
*/
function remove($recursive = true, $rmdir = false, $finderConfigFn = null);
function move($destination, $overwriteNewerFiles = true, $finderConfigFn = null);
function copy($destination, $overwriteNewerFiles = true, $finderConfigFn = null);
function touch($time = 0);
}
FileObj
class FileObj extends FilePath {
public $_mime = null;
public function reset() { $this->_mime = null; }
// string
public function mime();
public function mimeApache();
public function mimeExtension($default = 'bin');
// bool
public function isText();
public function isImage();
public function isBinary();
}
Test running
- install phpunit, composer, php if not installed
- run composer install at package dir
- run composer run-script test
On linux use *.sh files like *.bat files
🔵 Useful links
- Composer package generator
- Cmd windows
- PHP downloads
- PHP installations
- Git client
More features
For more features:
- Read source code and examples
- Practice with
Files
in real code