PHP code example of neunerlei / filesystem

1. Go to this page and download the library: Download neunerlei/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/ */

    

neunerlei / filesystem example snippets


use Neunerlei\FileSystem\Fs;
// Access the symfony methods as you would normally
Fs::getFs()->isAbsolutePath("...");

use Neunerlei\FileSystem\Fs;
// Copy a file
Fs::copy("/path/file.txt", "/anotherPath/file.txt");

// Copy / mirror a directory
Fs::copy("/path/to/directory", "/anotherPath");

use Neunerlei\FileSystem\Fs;
Fs::mkdir("/create/directory/recursively");

use Neunerlei\FileSystem\Fs;
Fs::exists("/check/existence.txt"); // TRUE|FALSE

use Neunerlei\FileSystem\Fs;
Fs::isReadable("/check/readability.txt"); // TRUE|FALSE

use Neunerlei\FileSystem\Fs;
Fs::isWritable("/check/writeability.txt"); // TRUE|FALSE

use Neunerlei\FileSystem\Fs;
Fs::isFile("/check/if/fileExists.txt"); // TRUE|FALSE

use Neunerlei\FileSystem\Fs;
Fs::isDir("/check/if/directoryExists.txt"); // TRUE|FALSE

use Neunerlei\FileSystem\Fs;
Fs::touch("/check/touchy.txt", new DateTime());

use Neunerlei\FileSystem\Fs;
Fs::remove("/path/to/remove.txt");

use Neunerlei\FileSystem\Fs;
Fs::flushDirectory("/path/to/clean");

use Neunerlei\FileSystem\Fs;
// Traverse the direct children of a directory
Fs::getDirectoryIterator("/path/to/iterate");

// Traverse a directory recursively
Fs::getDirectoryIterator("/path/to/iterate", true);

// Filter by regex
Fs::getDirectoryIterator("/path/to/iterate", true, ["regex" => "/\.txt$/"]);

// Return the folders before returning the children
Fs::getDirectoryIterator("/path/to/iterate", true, ["dirFirst"]);

use Neunerlei\FileSystem\Fs;
Fs::rename("/path/a", "/path/b");

use Neunerlei\FileSystem\Fs;
Fs::getPermissions("/file/with/full/access.txt"); // "0777"
Fs::getPermissions("/file/with/read/access.txt"); // "0222"

use Neunerlei\FileSystem\Fs;
Fs::setPermissions("/file/path.txt", 0222);
Fs::setPermissions("/directory", 0222);

use Neunerlei\FileSystem\Fs;
Fs::getOwner("/file/access.txt"); // 1000

use Neunerlei\FileSystem\Fs;
Fs::setOwner("/file/access.txt", 1001);

use Neunerlei\FileSystem\Fs;
Fs::getGroup("/file/access.txt"); // 5

use Neunerlei\FileSystem\Fs;
Fs::setGroup("/file/access.txt", 10);

use Neunerlei\FileSystem\Fs;
$content = Fs::readFile("/file/access.txt");

use Neunerlei\FileSystem\Fs;
$lines = Fs::readFileAsLines("/file/access.txt");

use Neunerlei\FileSystem\Fs;
Fs::writeFile("/file.txt", "myContent");

use Neunerlei\FileSystem\Fs;

// Add "myContent" as a new line to the file
Fs::appendToFile("/file.txt", "myContent");

// Add "myContent" directly at the end
Fs::appendToFile("/file.txt", "myContent", false);

use Neunerlei\FileSystem\Path;

// These methods are added by this fork
// ==========================================================
echo Path::unifySlashes("\\foo/bar\\baz");
// => /foo/bar/baz (on linux) or \foo\bar\baz (on windows)

echo Path::unifyPath("\\foo/bar\\baz");
// => /foo/bar/baz/ (on linux) or \foo\bar\baz\ (on windows)

echo Path::classBasename(\Neunerlei\FileSystem\Path::class);
// => Path

echo Path::classNamespace(\Neunerlei\FileSystem\Path::class);
// => Neunerlei\FileSystem

$link = Path::makeUri();
// => Returns a new Uri object -> See "URI" Section for details.

// Those methods were already in the base implementation
// ==========================================================
echo Path::canonicalize('/var/www/vhost/webmozart/../config.ini');
// => /var/www/vhost/config.ini

echo Path::canonicalize('C:\Programs\Webmozart\..\config.ini');
// => C:/Programs/config.ini

echo Path::canonicalize('~/config.ini');
// => /home/webmozart/config.ini

echo Path::makeAbsolute('config/config.yml', '/var/www/project');
// => /var/www/project/config/config.yml

echo Path::makeRelative('/var/www/project/config/config.yml', '/var/www/project/uploads');
// => ../config/config.yml

$paths = array(
    '/var/www/vhosts/project/httpdocs/config/config.yml',
    '/var/www/vhosts/project/httpdocs/images/banana.gif',
    '/var/www/vhosts/project/httpdocs/uploads/../images/nicer-banana.gif',
);

Path::getLongestCommonBasePath($paths);
// => /var/www/vhosts/project/httpdocs

Path::getFilename('/views/index.html.twig');
// => index.html.twig

Path::getFilenameWithoutExtension('/views/index.html.twig');
// => index.html

Path::getFilenameWithoutExtension('/views/index.html.twig', 'html.twig');
Path::getFilenameWithoutExtension('/views/index.html.twig', '.html.twig');
// => index

Path::getExtension('/views/index.html.twig');
// => twig

Path::hasExtension('/views/index.html.twig');
// => true

Path::hasExtension('/views/index.html.twig', 'twig');
// => true

Path::hasExtension('/images/profile.jpg', array('jpg', 'png', 'gif'));
// => true

Path::changeExtension('/images/profile.jpeg', 'jpg');
// => /images/profile.jpg

Path::join('phar://C:/Documents', 'projects/my-project.phar', 'composer.json');
// => phar://C:/Documents/projects/my-project.phar/composer.json

Path::getHomeDirectory();
// => /home/webmozart