1. Go to this page and download the library: Download ricwein/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/ */
ricwein / filesystem example snippets
use ricwein\FileSystem\Exceptions\FilesystemException;
use ricwein\FileSystem\File;
use ricwein\FileSystem\Storage;
try {
$file = new File(new Storage\Disk(__DIR__, '/test', '/dir2', 'test.json'));
$alt = new File(new Storage\Memory('{"some": "content"}'));
// read file into output-buffer
if ($file->isReadable()) {
header('Content-Type: ' . $file->getType(true));
echo $file->read();
} else {
header('Content-Type: ' . $alt->getType(true));
echo $alt->read();
}
} catch (FileSystemException $e) {
http_response_code(500);
echo json_encode(['errors' => [
'status' => $e->getCode(),
'title' => 'something went wrong',
'detail' => $e->getMessage(),
]]);
}
use ricwein\FileSystem\Directory;
use ricwein\FileSystem\Storage;
$originalDir = new Directory(new Storage\Disk(__DIR__));
$copyDir = new Directory($originalDir->storage());
$copyDir->cd('test'); // will also change $originalDir path!
use ricwein\FileSystem\Directory;
use ricwein\FileSystem\Storage;
$originalDir = new Directory(new Storage\Disk(__DIR__));
$copyDir = new Directory(clone $originalDir->storage());
$copyDir->cd('test'); // $originalDir will stay in __DIR__
use ricwein\FileSystem\File;
use ricwein\FileSystem\Storage;
$file = new File(new Storage\Disk(__DIR__, 'test.txt'));
$content = $file->read();
use ricwein\FileSystem\File;
use ricwein\FileSystem\Storage;
$file = new File(new Storage\Memory('some content'));
$content = $file->read();
use ricwein\FileSystem\File;
use ricwein\FileSystem\Storage;
$file = new File(new Storage\Stream(fopen('php://output', 'wb')));
$content = $file->write('content');
use League\Flysystem\Local\LocalFilesystemAdapter;
use ricwein\FileSystem\File;
use ricwein\FileSystem\Storage;
$file = new File(new Storage\Flysystem(new LocalFilesystemAdapter(__DIR__), 'test.txt'));
$content = $file->read();
use ricwein\FileSystem\Directory;
use ricwein\FileSystem\Storage;
$dir = new Directory(new Storage\Disk(__DIR__));
var_dump($dir->isReadable());
use ricwein\FileSystem\Directory;
use ricwein\FileSystem\Storage;
$hashes = [];
$dir = new Directory(new Storage\Disk(__DIR__));
foreach($dir->list(true)->files() as $file) {
$hashes[$file->getPath()->getFilename()] = $file->getHash();
}
use ricwein\FileSystem\Directory;
use ricwein\FileSystem\Helper\Constraint;
use ricwein\FileSystem\Storage;
$dir = new Directory(new Storage\Disk(__DIR__), Constraint::STRICT);
$file = $dir->file($_GET['filename']);
use ricwein\FileSystem\File;
use ricwein\FileSystem\Helper\Constraint;
use ricwein\FileSystem\Storage;
// let's assume $_GET['file'] == '/../file.txt'
// path concatenated as a single string
// this runs fine but is HIGHLY UNRECOMMENDED
$file = new File(new Storage\Disk(__DIR__ . $_GET['file']));
// path is passed as single parameters (comma instead of dot!)
// this throws an error since the resulting path is not within the safepath (__DIR__)
$file = new File(new Storage\Disk(__DIR__, $_GET['file']));
// however: disabling the safepath-constraint would also allow path traversal attacks:
$file = new File(new Storage\Disk(__DIR__, $_GET['file']), Constraint::STRICT & ~Constraint::IN_SAFEPATH);
use ricwein\FileSystem\Directory;
use ricwein\FileSystem\Helper\Constraint;
use ricwein\FileSystem\Storage;
$git = new Directory\Command(new Storage\Disk(__DIR__), Constraint::STRICT, ['/usr/local/bin/git', '/usr/bin/git']);
$ref = $git->execSafe('rev-parse HEAD');
use Intervention\Image\Image;
use ricwein\FileSystem\File;
use ricwein\FileSystem\Storage;
$image = new File\Image(new Storage\Disk('test.png'));
$image->resizeToFit(1024, 1024);
$image->compress(1048576); // iterative process to reduce filesize to be less than given filesize (1MB) by reducing the jpg-quality
// $image->encode('jpg');
$image->edit(function (Image $image): Image {
// add advanced image-manipulation here
[...]
return $image;
});
use ricwein\FileSystem\Directory;
use ricwein\FileSystem\File;
use ricwein\FileSystem\Storage;
$zip = new File\Zip(new Storage\Disk('archive.zip'));
// create zip file
$zip->add(new File(new Storage\Disk('file.json'))); // or $zip->addFile(...)
$zip->add(new File(new Storage\Memory('some file-content')), 'anotherfile.txt'); // or $zip->addFile(...)
$zip->add(new Directory(new Storage\Disk(__DIR__, 'data-dir'))); // or $zip->addDirectory(...)
$zip->commit();
// extract zip file
$extractDir = $zip->extractTo(new Storage\Disk\Temp);
use ricwein\FileSystem\File;
use ricwein\FileSystem\Storage;
$cert = new File\SSLCertificate(new Storage\File('certs/domain.crt'));
// OR
$cert = new File\SSLCertificate(new Storage\Memory('ssl://domain.com:443')); // or for HTTPS simply: 'domain.com'
// check if cert it currently valid (by timestamps)
$isValid = $cert->isValid();
// check if cert is also valid for a given host
$isValidForHost = $cert->isValidFor('subdomain.domain.com');
// show some certificate details
print_r([
'issuer' => $cert->getIssuer(),
'subject' => $cert->getValidDomains(),
'validFrom' => $cert->validFrom()->format('d.m.Y H:i:s'),
'validTo' => $cert->validTo()->format('d.m.Y H:i:s'),
]);
use ricwein\FileSystem\File;
use ricwein\FileSystem\Helper\Constraint;
use ricwein\FileSystem\Storage;
$current = new File(new Storage\Disk(getcwd(), 'file.json'), Constraint::STRICT & ~Constraint::IN_SAFEPATH);
// is the same as:
$current = new File(new Storage\Disk\Current('file.json'));
use ricwein\FileSystem\Directory;
use ricwein\FileSystem\Helper\Constraint;
use ricwein\FileSystem\Storage;
$git = new Directory\Command(new Storage\Disk\Current, Constraint::STRICT, ['/usr/local/bin/git', '/usr/bin/git']);
if (!$git->execSafe('pull $branch', ['branch' => 'develop'])) {
echo 'failed to execute: ' . $git->getLastCommand() . PHP_EOL;
}
exit($git->lastExitCode());
use ricwein\FileSystem\File;
use ricwein\FileSystem\Storage;
$temp = new File(new Storage\Disk\Temp);
$temp->write('test');
$temp->read();
$temp = null; // will delete the temp-file again!
use ricwein\FileSystem\File;
use ricwein\FileSystem\Storage;
$uploaded = new File(new Storage\Disk\Uploaded($_FILES['file']));
$file = $uploaded->moveTo(new Storage\Disk(__DIR__, 'uploads'));
use ricwein\FileSystem\File;
use ricwein\FileSystem\Storage;
$resource = fopen('test.json', 'rb');
$file = new File(new Storage\Memory\Resource($resource));
fclose($resource);
$content = $file->read();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.