PHP code example of phpdevcommunity / php-filesystem
1. Go to this page and download the library: Download phpdevcommunity/php-filesystem 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/ */
phpdevcommunity / php-filesystem example snippets
$base64Data = '...';
$tempFile = TempFile::fromBase64($base64Data);
$binaryData = '...';
$tempFile = TempFile::fromBinary($binaryData);
$resource = fopen('php://memory', 'w+');
fwrite($resource, 'test');
fseek($resource, 0);
$tempFile = TempFile::fromResource($resource);
$filePath = '/path/to/your/file.txt';
$fileInfo = new FileInfo($filePath);
$filename = $fileInfo->getFilename();
echo "Filename: $filename"; // Outputs: file.txt
$realPath = $fileInfo->getRealPath();
echo "Real Path: $realPath"; // Outputs: /absolute/path/to/file.txt
$size = $fileInfo->getSize();
echo "File Size: $size bytes";
$mimeType = $fileInfo->getMimeType();
echo "MIME Type: $mimeType"; // Example: text/plain
$extension = $fileInfo->getExtension();
echo "File Extension: $extension"; // Example: txt
$base64 = $fileInfo->toBase64();
echo "Base64 Encoded: $base64";
$dataUrl = $fileInfo->toDataUrl();
echo "Data URL: $dataUrl";
$binaryData = $fileInfo->toBinary();
echo "Binary Data: $binaryData";
$metadata = $fileInfo->getMetadata();
print_r($metadata);
/* Example Output:
[
'path' => '/absolute/path/to/file.txt',
'size' => 1024, // size in bytes
'mime_type' => 'text/plain',
'extension' => 'txt',
'basename' => 'file.txt',
'last_modified' => '2024-09-18 12:34:56',
'creation_date' => '2024-09-17 10:00:00'
]
*/
$fileInfo1 = new FileInfo('/path/to/file1.txt');
$fileInfo2 = new FileInfo('/path/to/file2.txt');
if ($fileInfo1->compareWith($fileInfo2)) {
echo "Files are identical";
} else {
echo "Files are different";
}
if ($fileInfo->delete()) {
echo "File deleted successfully";
} else {
echo "Failed to delete the file";
}
$fileObject = $fileInfo->openFile('r');
while (!$fileObject->eof()) {
echo $fileObject->fgets();
}
try {
$fileInfo = new FileInfo('/path/to/invalid/file.txt');
} catch (\RuntimeException $e) {
echo "Error: " . $e->getMessage();
}
$sourceDir = '/path/to/source';
$targetDir = '/path/to/target';
$synchronizer = new FileSynchronizer($sourceDir, $targetDir, function(array $info) {
echo sprintf("Action: %s, Source: %s, Target: %s\n", $info['action'], $info['source'], $info['target']);
});
$synchronizer->sync(true); // Recursive sync
$synchronizer->sync(false); // Non-recursive sync
$log = function(array $info) {
echo sprintf(
"Action: %s\nSource: %s\nTarget: %s\n",
$info['action'],
$info['source'],
$info['target']
);
};
$synchronizer = new FileSynchronizer($sourceDir, $targetDir, $log);
$synchronizer->sync(true);
$directoryPath = '/path/to/directory';
$explorer = new FileExplorer($directoryPath);
// Non-recursive listing
$files = $explorer->listAll(false);
// Recursive listing (including subdirectories)
$files = $explorer->listAll(true);
$files = $explorer->listAll(true);
foreach ($files as $file) {
echo $file['name'] . ($file['is_directory'] ? ' (Directory)' : ' (File)') . "\n";
}
$pattern = '*.txt'; // Example pattern to search for .txt files
$files = $explorer->searchByPattern($pattern, true); // Recursive search
$pattern = '*.html';
$htmlFiles = $explorer->searchByPattern($pattern, true);
foreach ($htmlFiles as $file) {
echo $file['path'] . " - Last modified: " . $file['modified_time'] . "\n";
}
// Search for all .txt files (non-recursive)
$txtFiles = $explorer->searchByExtension('txt', false);
// Search for all .html files recursively
$htmlFiles = $explorer->searchByExtension('html', true);
// Initialize FileExplorer with the desired directory
$explorer = new FileExplorer('/path/to/directory');
// List all files and directories (non-recursive)
$allFiles = $explorer->listAll(false);
foreach ($allFiles as $file) {
echo $file['name'] . ($file['is_directory'] ? ' (Directory)' : ' (File)') . "\n";
}
// Search for all .txt files (recursive)
$txtFiles = $explorer->searchByExtension('txt', true);
foreach ($txtFiles as $file) {
echo "Found .txt file: " . $file['path'] . "\n";
}
// Search for files that match a pattern (recursive)
$pattern = '*.log'; // Look for all .log files
$logFiles = $explorer->searchByPattern($pattern, true);
foreach ($logFiles as $file) {
echo "Found log file: " . $file['path'] . " - Size: " . $file['size'] . " bytes\n";
}
$fileInfo = new FileInfo('/path/to/large/file.txt');
$splitter = new FileSplitter($fileInfo);
$splitter = new FileSplitter($fileInfo, '/path/to/output/directory');
$files = $splitter->splitMb(1); // Split into 1 MB chunks
$splitFiles = $splitter->splitMb(5); // Split into 5 MB chunks
foreach ($splitFiles as $filePart) {
echo "Created part: " . $filePart->getRealPath() . "\n";
}
$files = $splitter->splitKb(512); // Split into 512 KB chunks
$splitFiles = $splitter->splitKb(200); // Split into 200 KB chunks
foreach ($splitFiles as $filePart) {
echo "Created part: " . $filePart->getRealPath() . "\n";
}
$chunkSizeInBytes = 1048576; // 1 MB in bytes
$files = $splitter->split($chunkSizeInBytes);
// Initialize FileInfo and FileSplitter
$fileInfo = new FileInfo('/path/to/large/file.txt');
$splitter = new FileSplitter($fileInfo);
// Split the file into 1 MB chunks
$splitFiles = $splitter->splitMb(1);
echo "File has been split into " . count($splitFiles) . " parts.\n";
bash
composer