A library for file management.
composer
composer require mmdm/sim-file
Or you can simply download zip file from github and extract it, then put file to your project library and use it like other libraries.
Just add line below to autoload files:
require_once 'path_to_library/autoloader.php';
and you are good to go.
It has three parts to use:
-
Download
-
Upload
-
FileSystem
// create new Download instance
$download = new Download($path_to_your_file);
$download->download($your_new_name);
// or just use static function
$download = Download::makeDownloadFromPath($path_to_your_file);
$download->download($your_new_name);
The download main method. If null passed as $name
, the original
file name will use as downloadable file name.
Get the path of file.
Set downloadable file name.
Get name of downloadable file without extension.
Set extension of downloadable file.
Get extension of downloadable file.
Get name of downloadable file with extension.
Set mimetype of downloadable file.
Get mimetype of downloadable file.
Get size of downloadable file.
Get size of downloadable file in a human readable format like 2MB or 4KB etc.
Make a download instance from a path with static calling.
To create a upload instance, you should pass a FileUpload
instance to Upload
constructor.
You should pass the key of $_FILES
that have uploaded file.
$fileUpload = new FileUpload($the_key_of_file_in_file_global_variable);
You can pass some validations to validate a file while upload method called.
Please refer to Validations section at the very bottom
Note: If unit is not specified, it'll be in bytes.
// new size validation instance
$sizeValidation = new SizeValidation('2MB', '1MB');
Get array of validations.
Get extension of uploaded file.
Get size of uploaded file.
Get original name of uploaded file without extension.
Get original name of uploaded file with extension.
Get errors of uploaded file. Basically it is standard error of uploaded file error and can have other errors too.
Note: Validation errors are not included here.
Add error to end of errors list.
Set error as a specific error in errors list.
Check if any error is set.
Translate standard errors to your locally language.
// create new Upload instance
$upload = new Upload();
$upload->upload($your_new_destination);
Do upload operation. Default behavior is to not overwrite existing file.
Set new name of uploaded file.
Get new name of uploaded file without extension. Default name is original name unless it change.
Get new name of uploaded file with extension.
Get errors during upload progress including validation errors.
Add error end of errors list.
Set error as a specific error in errors list.
Check if any error is set.
You can use all methods in both normal instantiable way or use static methods of file system.
These methods works on both files and directories.
Note: Some of these methods that work only with directories, and have a little comment on method documentation that says:
Works only on directories.
Check if specified file is exists.
Check if specified file is readable.
Check if specified file is writable.
Check if a path is file
Check if a path is directory
Check if a directory is empty or not.
Note: If you use file instead of directory, it'll use directory of that file to check.
Get all contents of a file as string
Alias of get
method.
Write some string data to a file and return number of bytes that were written to the file, or false on failure.
Alias of write
method.
Append some string data to a file and return number of bytes that were written to the file, or false on failure.
Append some string data to beginning of a file.
Copy a file to a new destination. Use true as $overwrite
parameter if want to overwrite the existence file.
Move a file to a new destination. Use true as $overwrite
parameter if want to overwrite the existence file.
Rename a file with new name. Use true as $overwrite
parameter if want to overwrite the existence file.
Do delete operation.
Note: It will delete folders recursively.
Do delete operation on filtered files.
Note: Only delete files in first level of a directory (This means no recursion)
Do delete operation on filtered files.
Note: It will delete folders recursively.
Change mode of a file.
Change owner/user of a file.
Change group of a file.
Get modification time of a file.
Change the touch time and access time of a file.
Get extension of a file or returns $prefer
.
Get mime type of a file or returns $prefer
.
Get user name of a file or false.
The user ID of the owner of the file, or false on failure.
Get file's group.
Get file's group id.
Get file's name.
Get file's basename.
Returns file's information. Information can be one or many of following constants (meaning of constant is obvious):
-
INFO_FILENAME
-
INFO_BASENAME
-
INFO_DIRNAME
-
INFO_SIZE
-
INFO_EXT
-
INFO_MIME_TYPE
-
INFO_IS_READABLE
-
INFO_IS_WRITABLE
-
INFO_OWNER
-
INFO_OWNER_ID
-
INFO_GROUP
-
INFO_GROUP_ID
Get size of a file.
Note: Size of directories will be calculated recursively.
Make a directory with a specific mode. To make directory recursively,
use true as $recursive
parameter.
Note: This method is only for directories.
Will return a RecursiveIteratorIterator
to iterate through all files
or null on error.
Note: This method is only for directories.
Note: If you want a file's files, it'll use directory of that file as needed directory to get files from.
Note: Please see this link
for more information about RecursiveIteratorIterator
methods.
An example of usage:
$files = $fileSystem->getAllFiles();
$files->rewind();
while ($files->valid()) {
/**
* @var SplFileInfo $file
*/
$file = $files->current();
// do whatever you need with that file
$files->next();
}
Will return an array of files of type SplFileInfo
from depth
of $depth
.
Note: This method is only for directories.
Note: If you want a file's files, it'll use directory of that file as needed directory to get files from.
Will return an array of filtered files of type SplFileInfo
from
depth of $depth
.
Note: This method is only for directories.
Note: If you want a file's files, it'll use directory of that file as needed directory to get files from.
Please refer to Filters section at the very bottom.
Get files of a specific directory as array of type SplFileInfo
.
Note: This method is only for directories.
Note: If you want a file's files, it'll use directory of that file as needed directory to get files from.
Get filtered files of a specific directory as array of
type SplFileInfo
.
Please refer to Filters section at the very bottom.
Note: This method is only for directories.
Note: If you want a file's files, it'll use directory of that file as needed directory to get files from.
Only difference of normal way and static way is, you should pass the file path you need to do the operation, to static methods.
See exists
in normal way.
See isReadable
in normal way.
See isWritable
in normal way.
See isFile
in normal way.
See isDir
in normal way.
See isEmpty
in normal way.
See get
in normal way.
See read
in normal way.
See put
in normal way.
See write
in normal way.
See append
in normal way.
See prepend
in normal way.
See copy
in normal way.
See move
in normal way.
See rename
in normal way.
See delete
in normal way.
See deleteFilteredFiles
in normal way.
See deleteAllFilteredFiles
in normal way.
See chmod
in normal way.
See chown
in normal way.
See chgrp
in normal way.
See modificationTime
in normal way.
See touch
in normal way.
See getExtension
in normal way.
See getMimeType
in normal way.
See getOwner
in normal way.
See getOwnerID
in normal way.
See getGroup
in normal way.
See getGroupID
in normal way.
See getName
in normal way.
See getBasename
in normal way.
See getInfo
in normal way.
See getSize
in normal way.
See mkdir
in normal way.
getDirAllFiles(string $dir, int $type = self::TYPE_FILE | self::TYPE_DIRECTORY): ?RecursiveIteratorIterator
See getAllFiles
in normal way.
getDirAllFilesInDepth(string $dir, int $depth = 0, int $type = self::TYPE_FILE | self::TYPE_DIRECTORY): array
See getAllFilesInDepth
in normal way.
See getAllFilteredFilesInDepth
in normal way.
See getFiles
in normal way.
See getFilteredFiles
in normal way.
To add your validation, you must extend from AbstractValidator
from Abstracts
directory and implement following method(s):
Main validation method that need a file of type FileUpload
.
To translate validations error(s) you need to use
setMessage($key, $message)
method inside you class after
extending AbstractValidator
and set your locally message to
specific error key.
Key of errors in each class are:
- ExtensionValidation
[
'extension' => 'Specified extension is not allowed!'
]
- MimeTypeValidation
[
'mimetype' => 'Specified mimetype is not allowed!',
]
- SizeValidation
[
'gt_size' => 'File size is greater than allowed file size!',
'lt_size' => 'File size is less than allowed file size!',
]
To add your filter, you must implement IFilter
interface from
Interfaces
directory and implement following method(s):
Main filter method that need a file of type SplFileInfo
.
Filter classes are:
- ExtensionFilter
// new extension filterer instance
// pass array of allowed extensions without dot
$extFilter = new ExtensionFilter(['png', 'jpg', 'jpeg']);
- MimeTypeFilter
// new mimetype filterer instance
// pass array of allowed mimetypes
$mimetypeFilter = new MimeTypeFilter(['image/png', 'image/jpg']);
- NameFilter
You should pass a regex according to name of file you need without extension to constructor.
// new name filterer instance
// files that name of them ends with 'es'
$nameFilter = new NameFilter('/es$/i');
- SizeFilter
You should pass max size as first parameter and min size as second parameter
Passed parameters can be in following format:
size of file with one of ['B', 'KB', 'MB', 'TB', 'PB']
Note: If unit is not specified, it'll be in bytes.
// new size filterer instance
$sizeFilter = new SizeFilter('2MB', '1MB');
- TypeFilter
There are two types: file and directory that can specify through constants below:
-
IFileSystem::TYPE_FILE
-
IFileSystem::TYPE_DIRECTORY
// new type filterer instance
$typeFilter = new TypeFilter(IFileSystem::TYPE_FILE | IFileSystem::TYPE_DIRECTORY);
- RegexFilter
You should pass a regex to filter a file and it'll apply on name and extension
// new regex filterer instance
$regexFilter = new RegexFilter('/es\.(png|jpe?g|gif)$/i');
Validations are as below for now:
- ExtensionValidation
// new extension validation instance
// pass array of allowed extensions without dot
$extValidation = new ExtensionValidation(['png', 'jpg', 'jpeg']);
- MimeTypeValidation
// new mimetype validation instance
// pass array of allowed mimetypes
$mimetypeValidation = new MimeTypeValidation(['image/png', 'image/jpg']);
- SizeValidation
You should pass max size as first parameter and min size as second parameter
Passed parameters can be in following format:
size of file with one of ['B', 'KB', 'MB', 'TB', 'PB']
Under MIT license.