PHP code example of dnj / local-filesystem

1. Go to this page and download the library: Download dnj/local-filesystem library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dnj / local-filesystem example snippets



use dnj\Filesystem\Local\File;

$file = new File('/etc/hosts');
$content = $file->read();
echo $content; // prints: 127.0.0.1       localhost 


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
}


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, ...
}


use dnj\Filesystem\Local\File;

$file = new File('/etc/hosts');
echo $file->size(); // prints the size of file in bytes


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();
}


use dnj\Filesystem\Local\File;

$file = new File('~/test-dnj-delete-file.txt');
$file->write('Hello World!');
$file->delete();


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!


use dnj\Filesystem\Local\Directory;

$dir = new Directory('~/dnj/');
$dir->exists(); // return bool


use dnj\Filesystem\Local\Directory;

$dir = new Directory('~/dnj/');
$dir->make();
// or:
$recursive = true;
$dir->make($recursive, 0700); // make recursivley with permission 0700


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;
    }
}


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;
    }
}


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;
    }
}


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;
    }
}


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 contents


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.txt


use dnj\Filesystem\Local\Directory;

$parent = new Directory('/tmp/');
$directory = $parent->file('test-directory');
$directory->make();

echo $directory->getPath(); // prints: /tmp/test-directory


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 content