Download the PHP package phpdevcommunity/php-filesystem without Composer
On this page you can find all versions of the php package phpdevcommunity/php-filesystem. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download phpdevcommunity/php-filesystem
More information about phpdevcommunity/php-filesystem
Files in phpdevcommunity/php-filesystem
Package php-filesystem
Short Description A lightweight PHP library for file system operations, including temporary file creation, file manipulation, and metadata handling using SPL.
License MIT
Informations about the package php-filesystem
PHP Filesystem
A lightweight PHP library for file system operations, including temporary file creation, file manipulation, and metadata handling using SPL.
Installation
You can install this library via Composer. Ensure your project meets the minimum PHP version requirement of 7.4.
Requirements
- PHP version 7.4 or higher
Table of Contents
- TempFile
- FileInfo
- FileSynchronizer
- FileExplorer
- FileSplitter
TempFile
The TempFile
class provides methods for creating temporary files from base64 data, binary data, and resources.
fromBase64
Create a temporary file from base64 encoded data.
fromBinary
Create a temporary file from binary data.
fromResource
Create a temporary file from a resource.
FileInfo
This guide covers how to use the FileInfo
class to manage files in PHP. The FileInfo
class allows for working with file metadata, converting files to various formats (base64, binary, data URLs), and performing file operations like comparison and deletion.
1. Instantiating the FileInfo
Class
To create a new instance of FileInfo
, pass the path of the file to its constructor.
This will throw an exception if the file does not exist or if the provided path is invalid.
2. Getting Basic File Information
Once you have an instance of FileInfo
, you can retrieve various details about the file:
-
Get Filename:
-
Get Real Path:
-
Get File Size:
-
Get MIME Type:
- Get File Extension:
3. Reading and Converting File Contents
You can easily convert the file's contents into different formats for storage or transmission:
-
Convert to Base64:
-
Convert to Data URL:
- Get File Content as Binary:
4. Working with File Metadata
Retrieve detailed metadata about the file:
5. Comparing Files
You can compare two files by content using their SHA-256 hash values:
6. File Deletion
To delete the file associated with the FileInfo
object:
Once the delete
method is called, the file is permanently removed from the filesystem.
7. Opening Files for Reading or Writing
You can open the file using SplFileObject
for reading or writing operations:
8. Error Handling
If the provided file path is invalid or the file does not exist, the constructor will throw a RuntimeException
. Always ensure that file paths are validated before instantiating the FileInfo
class:
FileSynchronizer
The FileSynchronizer
class provides an easy way to synchronize files between two directories, with optional recursive behavior. It copies files from the source directory to the target directory, ensuring that files are only copied if they are missing or have been updated.
1. Instantiating the FileSynchronizer
Class
To initialize the FileSynchronizer
, you need to provide the source directory, target directory, and an optional logging function to track operations.
Both the source and target must be valid directories, otherwise, the constructor will throw an InvalidArgumentException
.
2. Synchronizing Files
Once initialized, you can start synchronizing files from the source to the target directory using the sync()
method. If you want to synchronize recursively (i.e., include subdirectories), set the recursive
parameter to true
.
During synchronization, it copies any files from the source directory that are either missing or outdated in the target directory.
3. Logging Operations
You can pass a custom logging function to track the synchronization actions, such as copying files. The logging function receives an array of information containing the action (copy
), the source file path, and the target file path.
If no logging function is provided, the synchronization will proceed without logging any details.
4. Handling Directories
- Recursive Synchronization: If you choose to synchronize recursively, the
FileSynchronizer
will copy entire directory structures, ensuring all subdirectories and files are copied into the target directory. - Non-Recursive Synchronization: When the
recursive
flag is set tofalse
, only the files in the root of the source directory will be copied to the target, ignoring subdirectories.
5. Copying Files
The class only copies files when:
- The file does not already exist in the target directory.
- The source file has been modified after the file in the target directory.
Voici une documentation d’utilisation centrée uniquement sur les méthodes publiques de la classe FileExplorer
, qui pourrait être utile aux développeurs souhaitant utiliser cette classe dans leurs projets.
FileExplorer
The FileExplorer
class is a utility for exploring directories, listing files, and searching files based on patterns or extensions. This guide will walk through how to use its public methods for common file operations.
1. Instantiating the FileExplorer
Class
To start exploring a directory, you first need to instantiate the FileExplorer
class with a valid directory path.
Note: The constructor will throw an
InvalidArgumentException
if the provided path is not a valid directory.
2. Listing All Files and Directories: listAll()
The listAll()
method returns all files and directories within the specified directory. You can also explore subdirectories by setting the $recursive
flag to true
.
The result is an array where each item represents a file or directory. Each file or directory is provided as an associative array with these keys:
path
: The full path to the file or directory.name
: The name of the file or directory.is_directory
: Boolean indicating if it is a directory.size
: The file size in bytes (null for directories).modified_time
: Last modified timestamp.
Example usage:
3. Searching Files by Pattern: searchByPattern()
The searchByPattern()
method allows you to search for files that match a specific pattern (e.g., *.txt
for text files). You can perform the search recursively by setting the $recursive
flag to true
.
This method returns an array of files that match the pattern. The result format is the same as in listAll()
.
Example usage:
4. Searching Files by Extension: searchByExtension()
The searchByExtension()
method provides a simpler way to search for files by their extension. You only need to specify the extension, and it will internally use the searchByPattern()
method.
This method is ideal when you need to quickly filter files based on their extension without crafting a pattern.
5. Practical Example
Here’s a full example demonstrating how to use the FileExplorer
class to list all files in a directory and then search for files with specific extensions.
Voici une documentation d'utilisation centrée sur les méthodes publiques de la classe FileSplitter
pour les développeurs souhaitant diviser des fichiers en plusieurs morceaux.
FileSplitter
The FileSplitter
class allows developers to split large files into smaller parts, either by specifying the size in megabytes or kilobytes. This guide explains how to use its public methods to perform file splitting operations.
1. Instantiating the FileSplitter
Class
To start using the FileSplitter
, you need to instantiate it with a FileInfo
object representing the file you want to split. You can also specify a directory where the split parts will be saved, but if you don't, the parts will be saved in the same directory as the original file.
If you want to specify a different directory:
Note: The
FileInfo
class is required to provide file details. Make sure the file path is valid, and the file is readable.
2. Splitting the File by Megabytes: splitMb()
The splitMb()
method allows you to split a file into smaller parts based on the size in megabytes. Each part will have the specified size unless the file size is not divisible evenly.
The result is an array of FileInfo
objects, each representing a part of the original file.
Example usage:
3. Splitting the File by Kilobytes: splitKb()
If you prefer to specify the size in kilobytes, you can use the splitKb()
method. This works similarly to splitMb()
but operates in kilobytes.
This also returns an array of FileInfo
objects representing each part.
Example usage:
4. General File Splitting: split()
The split()
method is the core function that both splitMb()
and splitKb()
rely on. You can directly use this method to specify any custom size for the chunks in bytes.
Like the other methods, it returns an array of FileInfo
objects for the file parts.
5. Practical Example
Here’s a complete example that demonstrates how to split a file into parts and delete the parts afterward.
License
This library is open-source software licensed under the MIT license.