FileHandler
As lazy I am, when it comes to php function redundancy, I prefer to create my own tools rather than retyping the entire file open call over and over again., (*1)
I am using this self-made FileHandler for years now, and I still can't find a viable alternative., (*2)
Optional Requirements
Make sure to enable finfo (fileinfo) php extension to get mimetype and encoding in FileHandler::getInfo, (*3)
Installing
Via Composer:, (*4)
php composer.phar require martinmuzatko/filehandler
or add it as dependency:, (*5)
"require": {
"martinmuzatko/filehandler": "*"
}
Usage
Include the files or use autoloader., (*6)
use martinmuzatko\filehandler\File;
use martinmuzatko\filehandler\Handler as FileHandler;
Using the methods below, you can easily create, write, rename, read files.
While FileHandler is a set of static methods, File serves a convenient way of quickly modifying files., (*7)
FileHandler Examples
Checking Image Dimensions, (*8)
$file = 'path/to/image.jpg';
$info = FileHandler::getInfo($file);
if ($info->width > 1920 || $info->height > 1080)
{
echo 'File '.$info->basename.' is too big, resize it to 1920x1080';
}
File Examples
Batch creating customer directories, (*9)
$customers = ['01-jake', '02-mike', '03-francis', '04-martin', '05-jane'];
foreach ($customers as $customer)
{
$file = new File($customer.'/info.json');
$file
->create()
->write('[{ title: "Read instructions."}]')
->chmod(0644);
}
Constants
- TAB
- Linefeed (LF)
- Carriage Return (CR)
- Windows Combination (CRLF)
Methods
FileHandler (Static)
Methods are called statically (FileHandler::getInfo()), (*10)
getInfo($file)
Return fileinformation as object
Example:, (*11)
$info = FileHandler::getInfo($file); echo $info->writable;
Returns these informations:, (*12)
Paths, (*13)
- dirname
- basename
- extension
- filename
- path
Dimensions
* width
* height, (*14)
Timestamps and other Properties
* created
* modified
* size
* type
* mimetype (only with finfo enabled)
* encoding (only with finfo enabled), (*15)
Permissions, (*16)
Checks - Boolean
* writable
* readable
* exists
* isfile
* isdir
* islink, (*17)
getAsArray($file)
Returns file content as array., (*18)
read($file)
Return File as string if it exists, (*19)
write($file, $content, $seperator = CRLF)
Overwrites File with given string or array, returns true if succeeded., (*20)
create($file, $content = '')
Creates File but does not overwrite existing files., (*21)
delete($file)
Delete file if existing., (*22)
rename($old, $new)
Rename a File from old path to new path, (*23)
appendFile($targetFile, $file, $seperator = CRLF)
Appends $file to $targetFile with given seperator., (*24)
appendLine($targetFile, $content, $seperator = CRLF)
Appends $content to $targetFile with a defined $seperator, (*25)
clearFile($file)
Clears file from any content, (*26)
listFiles($path = '.', $includeFiles = true, $includeFolders = true)
List all files within a valid directory as array, (*27)
File (Method Chaining)
The constructor and some other methods accepts any of these:
* Instance of File
* String (Paths)
* Resources (Handles retrieved by fopen())
* Any other kind of File Stream, (*28)
After constructing, the File contains ANY info retrievable via FileHandler::getInfo()
e.g., (*29)
$f = new File('index.php'); $f->writable;
create()
Create file, accepts no param, file is entered via constructur:new File('path/to/file');, (*30)
copy($target)
delete()
rename($name)
move($target)
Moving file to desired location, will automatically create all directories needed for new location
example:, (*31)
$f = new File('index.php');
$f->move('path/to/new/');
will move index.php to path/to/new and will create the folders ./path, ./path/to and ./path/to/new, (*32)
chmod($octet)
Changing the file permissions from 0111 to 0777, (*33)
merge($target) work in progress
read()
Get file contents. Method chaining after calling this method is not possible anymore., (*34)
concat($content, $separator = CRLF)
Adding content to a file., (*35)
write($content)
Write as in overwrite (use concat if you want to add to the file instead of overwriting), (*36)
find($lookup)
Find files in a directory, regex enabled.
Retrievable via $file = new File(); $file->find(); $file->selection; or $file->getSelected(), (*37)
EXAMPLES:
Find by string:
find('item.png')
Find by Regex:
find('/[\w]*$/')
Find by Property - Value pairs with operators:
find(['mimetype' => 'image/png']) - find all files of mimetype image/png, (*38)
find(['size' => '<'.600*1024]) - find all files smaller than 600KB, (*39)
find(['created' => '>1441000000']) - fild all files newer than 31.08.2015, (*40)
find(['mimetype' => '!directory']) - fild all non-directory files, (*41)
Operators: <, >, <=, >=, !
Properties are used by getInfo()
get()
This method is used as final method to get selections made by find() or select() or new File()
Returns path if no selection is done., (*42)
select()
Selecting files by
paths
array of paths
array of Files
array of File Selections
You can also mix these, (*43)
Examples
$file->select('file.png');
$file->select(['file.png', 'another.jpg', 'file.avi']);, (*44)
$file->select([['file.png', 'another.jpg'], 'file.avi']);, (*45)
$file->select([$file->find(), 'customers/file.txt']);, (*46)
$file->select($file);
Any array or array of arrays will be traversed down to create an one-dimensional array saved to public property $selection.
Selections are retrievable by get(), (*47)