Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 82 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,28 @@ The package could be installed with composer:
composer require yiisoft/files --prefer-dist
```

## General usage
## FileHelper usage

Create a new directory:
FileHelper provides static methods you can use for various filesystem-related operations.

### Working with directories

Ensure a directory exists:

```php
use \Yiisoft\Files\FileHelper;

$directory = '/path/to/dir';
FileHelper::createDirectory($directory);
FileHelper::ensureDirectory($directory);
```

Create a new directory with the permission to be set:
Ensure a directory exists, and permission specified is set:

```php
use \Yiisoft\Files\FileHelper;

$directory = '/path/to/dir';
FileHelper::createDirectory($directory, 0775);
FileHelper::ensureDirectory($directory, 0775);
```

Remove a directory:
Expand All @@ -58,55 +62,102 @@ $directory = '/path/to/dir';
FileHelper::removeDirectory($directory);
```

Remove a file or symlink:
Remove everything within a directory but not directory itself:

```php
use \Yiisoft\Files\FileHelper;

$file = '/path/to/file.txt';
FileHelper::unlink($file);
$directory = '/path/to/dir';
FileHelper::clearDirectory($directory);
```

Normalize path:
Check if directory is empty:

```php
use \Yiisoft\Files\FileHelper;

$path = '/home/samdark/./test/..///dev\yii/';
echo FileHelper::normalizePath($path);
// outputs:
// /home/samdark/dev/yii
$directory = '/path/to/dir';
FileHelper::isEmptyDirectory($directory);
```

## File helper usage

File helper methods are static so usage is like the following:
Copy directory:

```php
\Yiisoft\Files\FileHelper::copyDirectory('/home/master/inbox', '/backup/inbox') ?>
use \Yiisoft\Files\FileHelper;

$source = '/path/to/source';
$destination = '/path/to/destination';
FileHelper::copyDirectory($source, $destination);
```

Overall the helper has the following method groups.
Additional options could be specified as third argument such as `filter` or `copyEmptyDirectories`.
Check method phpdoc for a full list of options.

### Working with directories
### Search

- createDirectory
- copyDirectory
- clearDirectory
- removeDirectory
- isEmptyDirectory
Searching for files:

### Search
```php
use \Yiisoft\Files\FileHelper;
use Yiisoft\Files\PathMatcher\PathMatcher;

$files = FileHelper::findFiles('/path/to/where/to/search', [
'filter' => (new PathMatcher())->only('*.png', '*.jpg')->except('logo.png'),
]);
```

Searching for directories:

```php
use \Yiisoft\Files\FileHelper;
use Yiisoft\Files\PathMatcher\PathMatcher;

- findDirectories
- findFiles
$directories = FileHelper::findDirectories('/path/to/where/to/search', [
'filter' => (new PathMatcher())->except('*meta'),
]);
```

### Other

- openFile
- normalizePath
- unlink
- lastModifiedTime
Open a file. Same as PHP's `fopen()` but throwing exceptions.

```php
use \Yiisoft\Files\FileHelper;

$handler = FileHelper::openFile('/path/to/file', 'rb');
```

Get last modified time for a directory or file:

```php
use \Yiisoft\Files\FileHelper;

$directory = '/path/to/dir';
$time = FileHelper::lastModifiedTime($directory);
```

The method is different from PHP's `filemtime()` because it actually scans a directory and selects the largest
modification time from all files.

Remove a file or symlink:

```php
use \Yiisoft\Files\FileHelper;

$file = '/path/to/file.txt';
FileHelper::unlink($file);
```

Normalize a path:

```php
use \Yiisoft\Files\FileHelper;

$path = '/home/samdark/./test/..///dev\yii/';
echo FileHelper::normalizePath($path);
// outputs:
// /home/samdark/dev/yii
```

## Testing

Expand Down
2 changes: 0 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
"config": {
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"test": "phpunit --testdox --no-interaction",
"test-watch": "phpunit-watcher watch"
Expand Down
Loading