PHP code example of iautomation / filesystem-helper

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

    

iautomation / filesystem-helper example snippets

public static function read($file)
public static function write($file, $contents, $flag='w+')
public static function delete($folder_or_file, $pattern=null)

use \FilesystemHelper\FilesystemHelper;

; // DELETE TEST AND ALL CONTENTS!!

use \FilesystemHelper\FilesystemHelper;

; // DELETE TEST AND ALL CONTENTS!!

// create test folder
FilesystemHelper::create('test');

// write to test/test.php
FilesystemHelper::write('test/test.php', 'test123');

// get file contents
echo FilesystemHelper::read('test/a.php');

// get all direct folders and files under test
foreach(FilesystemHelper::search('test') as $file){
	echo $file."\n";
}

// get all folders and files under test recursively
foreach(FilesystemHelper::searchR('test') as $file){
	echo $file."\n";
}

// get all direct files under test with the php extension using regex
foreach(FilesystemHelper::search('test', '/.*.php/') as $file){
	echo $file."\n";
}

// get all recursive files under test with the php extension using regex
foreach(FilesystemHelper::searchR('test', '/.*.php/') as $file){
	echo $file."\n";
}

// get all directories under test
foreach(FilesystemHelper::searchR('test') as $file){
	if($file->isDir())
		echo $file."\n";
}

// get all files under test
foreach(FilesystemHelper::searchR('test') as $file){
	if($file->isFile())
		echo $file."\n";
}

// get all folders and files under test listed "backwards". This is particularly useful when deleting
$iter = FilesystemHelper::searchR('test', null, -1, RecursiveIteratorIterator::CHILD_FIRST);
foreach($iter as $file){
	echo $file."\n";
}

/**
DELETE FUNCTIONS ARE NOT FORGIVING. USE WITH CAUTION
*/

// delete all folders and files under test recursively
FilesystemHelper::deleteR('test', null);

// delete all direct folders and files under test
FilesystemHelper::delete('test', null);

// delete all recursive files under test with the php extension using regex
FilesystemHelper::deleteR('test', '/.*.php/', 1);

// delete the test/test.php file
FilesystemHelper::delete('test/test.php');