PHP code example of effectra / fs

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

    

effectra / fs example snippets




ffectra\Fs\File;

// Example usage of the `exists()` method
$fileExists = File::exists('path/to/file.txt');
if ($fileExists) {
    echo "The file exists.";
} else {
    echo "The file does not exist.";
}

// Example usage of the `put()` method
$fileContent = 'Hello, world!';
$writeSuccess = File::put('path/to/newfile.txt', $fileContent);
if ($writeSuccess !== false) {
    echo "The file was successfully written.";
} else {
    echo "Failed to write the file.";
}

// Example usage of the `getContent()` method
$fileContent = File::getContent('path/to/file.txt');
echo "The file content is: " . $fileContent;

// Example usage of the `delete()` method
$deleteSuccess = File::delete('path/to/file.txt');
if ($deleteSuccess) {
    echo "The file was successfully deleted.";
} else {
    echo "Failed to delete the file.";
}

// ... Continue using other methods provided by the `File` class


use Effectra\Fs\Directory;

// Check if a path is a directory
$isDirectory = Directory::isDirectory('/path/to/directory');

// Create a new directory
$created = Directory::make('/path/to/new_directory');

// Delete a directory
$deleted = Directory::delete('/path/to/directory');

// Delete all directories within a directory
$deletedAll = Directory::deleteDirectories('/path/to/parent_directory');

// Get a directory instance
$dirInstance = Directory::instance('/path/to/directory');

// Copy a directory recursively to a destination
$copied = Directory::copy('/path/to/source_directory', '/path/to/destination_directory');

// Retrieve the name of a directory
$directoryName = Directory::name('/path/to/directory');

// Rename a directory
$renamed = Directory::rename('/path/to/old_directory', '/path/to/new_directory');

// Retrieve the full path of a directory's parent
$parentDirectory = Directory::fullName('/path/to/directory');

// Retrieve the files and directories within a directory
$entries = Directory::read('/path/to/directory');

// Retrieve the parent directory of a given directory path
$parentDirectory = Directory::parent('/path/to/directory');

// Check if a directory is considered private
$isPrivate = Directory::isPrivate('/path/to/directory');

// Retrieve the files within a directory
$files = Directory::files('/path/to/directory', true, ['txt', 'csv']);

// Check if a directory has any files
$hasFiles = Directory::hasFiles('/path/to/directory');

// Delete files within a directory
$deletedFiles = Directory::deleteFiles('/path/to/directory', ['txt', 'csv']);

// Retrieve the directories within a directory
$directories = Directory::directories('/path/to/directory', true);

// Empty a directory by deleting its files and subdirectories
Directory::empty('/path/to/directory');

$separator = \Effectra\Fs\Path::ds();
echo $separator; // Outputs '\' on Windows, '/' on Unix-like systems

$path = \Effectra\Fs\Path::format('path/to//file');
echo $path; // Outputs 'path/to/file' on all platforms

$path = \Effectra\Fs\Path::removeExtension('file.txt');
echo $path; // Outputs 'file'

$path = \Effectra\Fs\Path::setExtension('file', 'txt');
echo $path; // Outputs 'file.txt'

$fileData = new \Effectra\Fs\FileData();
$dataUrl = $fileData->fileToDataUrl('/path/to/file.jpg');
if ($dataUrl !== false) {
    echo $dataUrl;
} else {
    echo "Failed to convert the file to a data URL.";
}

$fileData = new \Effectra\Fs\FileData();
$result = $fileData->dataUrlToFile('data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAAAAAAAD...', '/path/to/output.jpg');
if ($result) {
    echo "File saved successfully.";
} else {
    echo "Failed to save the file.";
}

use Effectra\Fs\FileEncryption;

// Create an instance of FileEncryption with the encryption key
$fileEncryption = new FileEncryption('your-encryption-key');

// Encrypt a file
$fileEncryption->encryptFile('path/to/source/file.txt', 'path/to/destination/encrypted-file.txt');

// Decrypt a file
$fileEncryption->decryptFile('path/to/source/encrypted-file.txt', 'path/to/destination/decrypted-file.txt');


use Effectra\Fs\FileEncryption;

$fileEncryption = new FileEncryption('my-secret-key');

// Encrypt a file
$fileEncryption->encryptFile('data.txt', 'encrypted-data.txt');

// Decrypt a file
$fileEncryption->decryptFile('encrypted-data.txt', 'decrypted-data.txt');