1. Go to this page and download the library: Download tobento/service-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/ */
tobento / service-filesystem example snippets
use Tobento\Service\Filesystem\File;
$file = new File('home/public/src/foo/image.jpg');
var_dump($file->isFile()); // bool(true)
$file = new File('home/public/src/foo');
var_dump($file->isFile()); // bool(false)
use Tobento\Service\Filesystem\File;
$file = new File('home/public/src/foo/image.jpg');
var_dump($file->isExtension(['jpeg', 'tiff'])); // bool(true)
var_dump($file->isImage(['jpeg', 'gif'])); // bool(true)
var_dump($file->isHtmlImage()); // bool(true)
// $file->isHtmlImage() is same as:
$file->isImage(['gif', 'jpeg', 'png', 'webp']);
use Tobento\Service\Filesystem\File;
$file = new File('home/public/src/foo/image.jpg');
$newFile = $file->withDirname('home/public/src/bar/');
$newFile = $file->withFilename('new-image');
// Defining a folder path might be useful for certain cases.
$newFile = $file->withFolderPath('foo/bar');
// Generating unique filename.
$file = $file->withUniqueFilename();
var_dump($file->getBasename()); // string(18) "image-20317715.jpg"
var_dump($file->isFile()); // bool(false)
use Tobento\Service\Filesystem\File;
$file = new File('home/public/src/foo/image.jpg');
$copiedFile = $file->copy('home/public/src/bar/name.jpg');
$movedFile = $file->move('home/public/src/bar/name.jpg');
$renamedFile = $movedFile->rename('new-name');
var_dump($movedFile->delete()); // bool(true)
use Tobento\Service\Filesystem\File;
$file = new File('home/public/src/foo/image.jpg');
var_dump($file->isWithinDir('home/public/src'));
// bool(true)
var_dump($file->isWithinDir('home/public/src/foo/'));
// bool(true)
var_dump($file->isWithinDir('home/public/src/bar/'));
// bool(false)
use Tobento\Service\Filesystem\File;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamFactoryInterface;
$file = new File('home/public/src/foo/image.jpg');
$downloadResponse = $file->downloadResponse($response, $streamFactory);
$fileResponse = $file->fileResponse($response, $streamFactory);
use Tobento\Service\Filesystem\JsonFile;
$file = new JsonFile('home/public/src/foo/data.json');
var_dump($file->isJson()); // bool(true)
$array = $file->toArray();
$jsonString = $file->getContent();
use Tobento\Service\Filesystem\PdfFile;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamFactoryInterface;
$file = new Pdf('home/public/src/foo/data.pdf');
var_dump($file->isPdf()); // bool(true)
// Content example
$file = new PdfFile('document.pdf');
var_dump($file->isPdf()); // bool(false)
// add content, this will return a new instance.
$file = $file->content($pdfEngine->createPdfString('document.pdf'));
$string = $file->getContent();
$fileResponse = $file->fileResponse($response, $streamFactory);
use Tobento\Service\Filesystem\Dir;
$dir = new Dir();
var_dump($dir->isDir('home/public/src/foo')); // bool(true)
var_dump($dir->isDir('home/public/src/foo/')); // bool(true)
var_dump($dir->isFile('home/public/src/foo/image.jpg')); // bool(true)
var_dump($dir->isWritable(__DIR__.'/src/foo')); // bool(true)
// Check if directory or file exists.
var_dump($dir->has(__DIR__.'/src/foo')); // bool(true)
var_dump($dir->has(__DIR__.'/src/foo/')); // bool(true)
var_dump($dir->has(__DIR__.'/src/foo/image.jpg')); // bool(true)
// Check if a directory is empty.
var_dump($dir->isEmpty(__DIR__.'/src/foo/')); // bool(false)
use Tobento\Service\Filesystem\Dir;
use Tobento\Service\Filesystem\File;
$dir = new Dir();
$dir->setFilesToIgnore(['.DS_Store', '*_notes', 'Thumbs.db', 'folder/subdir/image.png']); // use wildcard * for all
$dir->setFilesToIgnoreExtensions(['htaccess', 'txt']);
$files = $dir->getFiles('home/public/media/path/folder/');
// define a folder path, might be useful for certain use cases.
$files = $dir->getFiles('home/public/media/path/folder/', 'path/folder/');
// only get png and jpeg files.
$files = $dir->getFiles('home/public/media/path/folder/', '', ['png', 'jpeg']);
foreach($files as $file)
{
var_dump($file instanceof File); // bool(true)
}
use Tobento\Service\Filesystem\Dir;
use Tobento\Service\Filesystem\Folder;
$dir = new Dir();
$dir->setFoldersToIgnore(['img/firm', 'misc/test', '*_notes']); // use wildcard * for all
// get all with subfolders.
$folders = $dir->getFoldersAll('home/public/media/');
// get only first level folders. Note: $dir->setFoldersToIgnore() has no influence.
$folders = $dir->getFolders('home/public/media/');
foreach($folders as $folder)
{
var_dump($folder instanceof Folder); // bool(true)
// Get folder information
$name = $folder->name();
$dir = $folder->dir();
$id = $folder->id();
$parentId = $folder->parentId();
$level = $folder->level();
$folderPath = $folder->folderPath();
// Manipulate, returns a new instance.
$folder = $folder->withName('-'.$folder->name());
$folder = $folder->withFolderPath('bar/foo');
}
use Tobento\Service\Filesystem\Dir;
$dir = new Dir();
var_dump($dir->create('home/public/media/new/', mode: 0755, recursive: true)); // bool(true)
var_dump($dir->rename('home/public/media/old/', 'new')); // bool(true)
use Tobento\Service\Filesystem\Dir;
$dir = new Dir();
$dir->setFilesToIgnore(['subdir/image.png']); // starting from dir set.
$dir->setFilesToIgnoreExtensions(['txt']);
$dir->setFoldersToIgnore(['sub/foo']); // starting from dir set.
var_dump($dir->copy(dir: 'home/public/media/foo/', destination: 'home/public/media/bar/')); // bool(true)
use Tobento\Service\Filesystem\Dir;
$dir = new Dir();
// Careful: this will delete all subfolders and all files.
var_dump($dir->delete('www/public/media/dir_to_delete/')); // bool(true)
// This method will delete files too.
var_dump($dir->delete('www/public/media/file_to_delete.jpg')); // bool(true)
use Tobento\Service\Filesystem\Dir;
$dir = new Dir(withDefaultFormats: false);
// would only check txt files by its extension as withDefaultFormats is set to false.
$dir->setFilesToIgnoreExtensions(['txt']);
use Tobento\Service\Filesystem\Dir;
use Tobento\Service\Filesystem\FileFormatsInterface;
$dir = new Dir();
var_dump($dir instanceof FileFormatsInterface); // bool(true)
$dir->addFormat(format: 'jpeg', mimeType: 'image/jpeg');
$dir->addFormat('jpg', 'image/jpeg');
$dir->addFormat('csv', 'text/csv');
$dir->addFormat('csv', 'text/plain');
use Tobento\Service\Filesystem\Dir;
$dir = new Dir();
$dir->mergeFormats([
'jpeg' => ['image/jpeg'],
'csv' => ['text/x-csv'],
'gif' => ['image/gif'],
]);
use Tobento\Service\Filesystem\Dir;
$dir = new Dir(withDefaultFormats: false);
$dir->addFormat('jpeg', 'image/jpeg');
$dir->addFormat('jpg', 'image/jpeg');
// Returns first found format or null if not found any.
var_dump($dir->getFormat(mimeType: 'image/jpeg'));
// string(4) "jpeg"
use Tobento\Service\Filesystem\Dir;
$dir = new Dir(withDefaultFormats: false);
$dir->addFormat('jpeg', 'image/jpeg');
$dir->addFormat('jpg', 'image/jpeg');
// Returns first found mime type or null if not found any.
var_dump($dir->getMimeType(format: 'jpg'));
// string(10) "image/jpeg"
// Return part one
var_dump($dir->getMimeType('jpg', part: 1));
string(5) "image"
// Return part two
var_dump($dir->getMimeType('jpg', 2));
string(4) "jpeg"
use Tobento\Service\Filesystem\Dir;
$dir = new Dir(withDefaultFormats: false);
$dir->addFormat('jpeg', 'image/jpeg');
$dir->addFormat('csv', 'text/csv');
$mimeTypes = $dir->getMimeTypes(formats: ['jpeg', 'csv']);
/*
Array
(
[0] => image/jpeg
[1] => text/csv
)
*/
// part one only
$mimeTypes = $dir->getMimeTypes(formats: ['jpeg', 'csv'], part: 1);
/*
Array
(
[0] => image
[1] => text
)
*/
// part two only
$mimeTypes = $dir->getMimeTypes(formats: ['jpeg', 'csv'], part: 2);
/*
Array
(
[0] => jpeg
[1] => csv
)
*/
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.