PHP Classes

PHP Filesystem Helper Library: Manipulate and search files and directories

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 253 All time: 7,887 This week: 75Up
Version License PHP version Categories
filesystem-helper 0.1GNU General Publi...5PHP 5, Files and Folders
Description 

Author

This class can manipulate and search files and directories.

It can create directories, read and write, as well and delete files and folders recursively using PHP iterator classes.

The class can also search for files inside given directories recursively using regular expressions to filter the file names.

Picture of Joshua McKenzie
  Performance   Level  
Name: Joshua McKenzie <contact>
Classes: 2 packages by
Country: United States United States
Age: ???
All time rank: 3247436 in United States United States
Week rank: 214 Up28 in United States United States Up

Recommendations

Find Large Files
I need to find large files on a server

list files and folders
list files and folders in a directory

Documentation

PHP-Filesystem-Helper

A helper class for filesystem operations

This class uses PHP Iterators in order to dynamically and stably provide filesystem operation access.

Introduction

I tried to make this as fluid and dynamic as possible. Strangely, I couldn't seem to find any presently existing dynamic classes for file io operations, so I spent a couple hours coding this up. Please DO report possible errors.

I think the most important note is that Iterators do not take up massive amounts of memory like arrays do provided with glob and other methods. This class uses Iterators. Thus, we have speed + scalability.

Some might wonder why I did not implement glob methods. Two very good reasons led me to avoid glob: 1. Glob returns all results in an array, and has a relatively small limit(10,000 results or so). 2. Regex covers what glob provides, + more

That's all. Enjoy.

Overview

The following methods are provided:

  • 
    
  • 
    
  • 
    
  • 
    

Installation

The class can be used standalone, however it's recommended to install via Composer:

composer require iautomation/filesystem-helper

Usage

If using Composer, just do something like:

use \FilesystemHelper\FilesystemHelper;

include 'path/to/vendor/autoload.php';

FilesystemHelper::deleteR('test', null); // DELETE TEST AND ALL CONTENTS!!

Otherwise, simply include the class:

use \FilesystemHelper\FilesystemHelper;

include 'path/to/FilesystemHelper.php';

FilesystemHelper::deleteR('test', null); // DELETE TEST AND ALL CONTENTS!!

Examples

// 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');

  Files folder image Files (12)  
File Role Description
Files folder imagesrc (1 directory)
Files folder imagevendor (1 file, 1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (12)  /  src  
File Role Description
Files folder imageFilesystemHelper (1 file)

  Files folder image Files (12)  /  src  /  FilesystemHelper  
File Role Description
  Plain text file FilesystemHelper.php Class Class source

  Files folder image Files (12)  /  vendor  
File Role Description
Files folder imagecomposer (7 files)
  Accessible without login Plain text file autoload.php Appl. Application script

  Files folder image Files (12)  /  vendor  /  composer  
File Role Description
  Accessible without login Plain text file autoload_classmap.php Appl. Application script
  Accessible without login Plain text file autoload_namespaces.php Appl. Application script
  Accessible without login Plain text file autoload_psr4.php Appl. Application script
  Accessible without login Plain text file autoload_real.php Appl. Application script
  Accessible without login Plain text file ClassLoader.php Appl. Application script
  Accessible without login Plain text file installed.json Data Auxiliary data
  Accessible without login Plain text file LICENSE Lic. License text

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:253
This week:0
All time:7,887
This week:75Up