PHP code example of libratechie / think-filesystem
1. Go to this page and download the library: Download libratechie/think-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/ */
libratechie / think-filesystem example snippets
use \Libratechie\Think\Filesystem;
$disk = Filesystem::disk('public');
// Write files
$folderPath = '/path/to';
$file = request()->file('file');
// Use the default naming convention to write the file
$fileName = $disk->putFile($folderPath, $file);
// $fileName: /path/to/20240725/2697c763c84fe48d0166d0cd37181e19.jpg
// Use SHA-256 hash as the file name
$fileName = $disk->putFile($folderPath, $file, 'sha256');
// $fileName: /path/to/55/fd6b615cb02ce73c8e708ac62c9fe9c0cdd92d9161c57186e592d2b672e6e3.jpg
// Use a custom callback function to generate the file name
$fileName = $disk->putFile($folderPath, $file, function ($fileHash) {
return 'custom' . DIRECTORY_SEPARATOR . md5($fileHash->getPathname());
});
// $fileName: /path/to/custom/db8fbf2c977c3fae6276521f788d5183.jpg
// Save the file with a specified file name
$fileName = $disk->putFileAs($folderPath, $file, 'custom.txt');
// Check whether a file exists.
$exists = $disk->fileExists('/path/to/custom.txt');
// Get file access path.
$exists = $disk->url('/path/to/custom.txt');
// Get file size.
$size = $disk->fileSize('/path/to/custom.txt');
// Get file mimeType.
$size = $disk->mimeType('/path/to/custom.txt');