This is a simple implemenetation of DNJ\FileSystem for local disk based file system.
- Latest versions of PHP and PHPUnit and PHPCsFixer
- Best practices applied:
README.md(badges included)LICENSEcomposer.jsonphpunit.xml.gitignore.php-cs-fixer.php
- Some useful resources to start coding
This is an implementation of local disk filesystem that allows you to work with local files.
First of all, you need to add this library to your project, so run:
composer require dnj/local-filesystemRead file:
<?php
use dnj\Filesystem\Local\File;
$file = new File('/etc/hosts');
$content = $file->read();
echo $content; // prints: 127.0.0.1 localhost Write file:
<?php
use dnj\Filesystem\Local\File;
$file = new File('~/adele-hello.txt');
$lyrics = <<<EOF
Hello, it's me
I was wondering if after all these years you'd like to meet
To go over everything
They say that time's supposed to heal ya, but I ain't done much healing
EOF;
try {
$file->write($lyrics);
} catch (IOException $e) {
// in case failed for any reason, like permission denied
}Rename file:
<?php
use dnj\Filesystem\Local\File;
$file = new File('~/adele-hello.txt');
$lyrics = <<<EOF
I heard that you're settled down
That you found a girl and you're married now
I heard that your dreams came true
Guess she gave you things, I didn't give to you
Old friend, why are you so shy?
EOF;
try {
$file->write($lyrics);
} catch (IOException $e) {
// in case failed for any reason, like permission denied, ...
}
try {
$file->rename('adele-someone-like-you.txt');
// or you can completly move this file to new dest:
$destFile = new File('/tmp/adele-someone-like-you.txt');
$file->move($destFile);
} catch (IOException $e) {
// in case failed for any reason, like permission denied, ...
}Get size of file:
<?php
use dnj\Filesystem\Local\File;
$file = new File('/etc/hosts');
echo $file->size(); // prints the size of file in bytesExistence of file and touch it (create it) if not exists:
<?php
use dnj\Filesystem\Local\File;
$file = new File('~/dnj-does-file-exists.txt');
echo 'file ' . ($file->exists() ? 'exists.' : 'not exists!');
if ($file->exists()) {
echo 'File exists.' . PHP_EOL;
} else {
echo 'File does not exists!' . PHP_EOL;
// touch file
$file->touch();
}Delete file:
<?php
use dnj\Filesystem\Local\File;
$file = new File('~/test-dnj-delete-file.txt');
$file->write('Hello World!');
$file->delete();Append to file:
<?php
use dnj\Filesystem\Local\File;
$file = new File('~/hello-world.txt');
$file->write('Hello');
echo $file->read(); // prints: Hello
$file->append(' World!');
echo $file->read(); // prints: Hello World!Check directory existence:
<?php
use dnj\Filesystem\Local\Directory;
$dir = new Directory('~/dnj/');
$dir->exists(); // return boolMake directory:
<?php
use dnj\Filesystem\Local\Directory;
$dir = new Directory('~/dnj/');
$dir->make();
// or:
$recursive = true;
$dir->make($recursive, 0700); // make recursivley with permission 0700Get files of directory:
<?php
use dnj\Filesystem\Local\Directory;
$dir = new Directory('/tmp/');
if ($dir->exists()) {
// $directory->file() returns Generator of dnj\Filesystem\Local\File;
$recursive = false;
foreach ($directory->files($recursive) as $file) {
echo $file->getPath() . PHP_EOL;
}
}Get directories of directory:
<?php
use dnj\Filesystem\Local\Directory;
$parent = new Directory('/tmp/');
if ($dir->exists()) {
// $directory->directories() returns Generator of dnj\Filesystem\Local\Directory;
$recursive = false;
foreach ($parent->directories($recursive) as $directory) {
echo $directory->getPath() . PHP_EOL;
}
}Get directories of directory:
<?php
use dnj\Filesystem\Local\Directory;
$dir = new Directory('/tmp/');
if ($dir->exists()) {
// $directory->file() returns Generator of dnj\Filesystem\Local\File;
$recursive = false;
foreach ($directory->files($recursive) as $file) {
echo $file->getPath() . PHP_EOL;
}
}Get items (files and directories) of directory:
<?php
use dnj\Filesystem\Local\Directory;
$dir = new Directory('/tmp/');
if ($dir->exists()) {
// $directory->file() returns Generator of dnj\Filesystem\Local\File|dnj\Filesystem\Local\Directory;
$recursive = false;
foreach ($directory->items($recursive) as $node) {
echo $node->getPath() . PHP_EOL;
}
}Get size of a directory:
<?php
use dnj\Filesystem\Local\Directory;
$dir = new Directory('/tmp/');
$recursively = true;
echo $dir->size($recursively); // prints the size of directory and all of it's contentsGet file from directory:
<?php
use dnj\Filesystem\Local\Directory;
$parent = new Directory('/tmp/');
$file = $parent->file('test-file.txt');
$file->write('https://dnj.co.ir');
echo $file->getPath(); // prints: /tmp/test-file.txtGet directory from directory:
<?php
use dnj\Filesystem\Local\Directory;
$parent = new Directory('/tmp/');
$directory = $parent->file('test-directory');
$directory->make();
echo $directory->getPath(); // prints: /tmp/test-directoryDelete directory:
<?php
use dnj\Filesystem\Local\Directory;
$parent = new Directory('/tmp/');
$directory = $parent->file('test-directory');
$directory->make();
echo $directory->getPath(); // prints: /tmp/test-directory
$directory->delete(); // delete directory and all of it's contentWe'll try to maintain this project as simple as possible, but Pull Requests are welcomed!
The MIT License (MIT). Please see License File for more information.