PHP code example of gungcahyadipp / file-service

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

    

gungcahyadipp / file-service example snippets


'providers' => [
    // ...
    GungCahyadiPP\FileService\Providers\FileServiceProvider::class,
],

'aliases' => [
    // ...
    'FileService' => GungCahyadiPP\FileService\Facades\FileService::class,
],

use GungCahyadiPP\FileService\Facades\FileService;

// Get file URL
$url = FileService::getFile('documents', 'my-file.pdf');

// Save uploaded file
$fileName = FileService::saveFile($uploadedFile, 'images', 'my-image');

// Delete file
$deleted = FileService::deleteFile('images', 'my-image.jpg');

use GungCahyadiPP\FileService\FileService;

class DocumentController extends Controller
{
    protected $fileService;

    public function __construct(FileService $fileService)
    {
        $this->fileService = $fileService;
    }

    public function upload(Request $request)
    {
        $file = $request->file('document');
        $fileName = $this->fileService->saveFile($file, 'documents', 'my-document');
        
        return response()->json(['filename' => $fileName]);
    }
}

$fileInfo = FileService::saveFileComplete(
    $uploadedFile, 
    'images', 
    'profile-picture',
    true // Enable compression
);

// Returns:
// [
//     'title' => 'profile-picture',
//     'name' => 'profile-picture-abc1.jpg',
//     'path' => 'images/profile-picture-abc1.jpg',
//     'size' => 1024000,
//     'mime_type' => 'image/jpeg',
//     'ext' => 'jpg',
//     'disk' => 'local',
//     'url' => 'http://localhost/storage/images/profile-picture-abc1.jpg'
// ]

$base64Image = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIA...';
$fileName = FileService::saveBase64Image($base64Image, 'avatars', 'user-avatar');

if ($fileName) {
    echo "Image saved as: " . $fileName;
} else {
    echo "Failed to save image";
}

// Compress images automatically during upload
$fileInfo = FileService::saveFileComplete(
    $uploadedFile,
    'thumbnails',
    'product-image',
    true // Enable compression
);

FileService::compressImage(
    '/path/to/image.jpg',
    85,    // Quality (1-100)
    800,   // Max width
    600    // Max height
);

use GungCahyadiPP\FileService\FileService;

// Use specific disk
$s3FileService = new FileService('s3');
$localFileService = new FileService('local');

// Save to S3
$s3FileName = $s3FileService->saveFile($file, 'documents', 'contract');

// Save to local storage
$localFileName = $localFileService->saveFile($file, 'temp', 'upload');

$fileName = FileService::saveFile($uploadedFile, 'images', 'photo');

if ($fileName) {
    // File saved successfully
    $url = FileService::getFile('images', $fileName);
} else {
    // Handle upload failure
    Log::error('Failed to upload file');
}
bash
php artisan vendor:publish --tag=file-service-config