PHP code example of vldmir / laravel-temp-file-manager

1. Go to this page and download the library: Download vldmir/laravel-temp-file-manager 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/ */

    

vldmir / laravel-temp-file-manager example snippets


return [
    'directory' => 'temp',
    'max_age_hours' => 10,
    'disk' => 'local',
];

// Save string content
$content = "Hello, World!";
$tempPath = TempManager::save($content, 'hello.txt');

// Save with auto-generated filename
$tempPath = TempManager::save($content);

// In your controller
public function upload(Request $request)
{
    // Save uploaded file with original name
    $tempPath = TempManager::saveUploadedFile($request->file('document'));
    
    // Save with custom filename
    $tempPath = TempManager::saveUploadedFile(
        $request->file('document'), 
        'custom-name.pdf'
    );
}

// Download and save file from URL
try {
    $tempPath = TempManager::saveFromUrl('https://example.com/file.pdf');
    
    // With custom filename
    $tempPath = TempManager::saveFromUrl(
        'https://example.com/file.pdf', 
        'local-copy.pdf'
    );
} catch (\Exception $e) {
    // Handle download error
}

// Save from resource
$handle = fopen('path/to/file', 'r');
$tempPath = TempManager::save($handle, 'output.txt');
fclose($handle);

// The file will be automatically deleted when the PHP process ends
$tempPath = TempManager::getTempPath('upload.txt');
TempManager::register($tempPath);

// Do your work with the file
// ...
// File will be deleted automatically after process completion

// Manually delete when you're done
TempManager::cleanup($tempPath);

// All files older than max_age_hours will be removed
TempManager::cleanupOldFiles();

protected function schedule(Schedule $schedule)
{
    $schedule->command('temp-files:cleanup')->hourly();
}

use Vldmir\TempFileManager\TempFileManager;

class MyController extends Controller
{
    public function __construct(private TempFileManager $tempManager)
    {
        $this->tempManager = $tempManager;
    }

    public function store(Request $request)
    {
        $tempPath = $this->tempManager->getTempPath('uploaded-file.txt');
        $this->tempManager->register($tempPath);
        // File will be auto-cleaned after process ends
    }
}

class DocumentController extends Controller
{
    public function store(Request $request)
    {
        try {
            // Save uploaded file
            $tempPath = TempManager::saveUploadedFile($request->file('document'));
            
            // Process the file
            // ...
            
            // Optionally clean up early if you're done
            TempManager::cleanup($tempPath);
            
            return response()->json(['success' => true]);
            
        } catch (\Exception $e) {
            // File will be auto-cleaned up when process ends
            return response()->json(['error' => $e->getMessage()], 500);
        }
    }
    
    public function download()
    {
        try {
            // Save remote file temporarily
            $tempPath = TempManager::saveFromUrl('https://example.com/document.pdf');
            
            // Process or serve the file
            return response()->download($tempPath);
            // File will be cleaned up after response is sent
            
        } catch (\Exception $e) {
            return response()->json(['error' => $e->getMessage()], 500);
        }
    }
}

// Examples of filename handling
$manager = app(TempFileManager::class);

// With custom filename (unsafe characters are removed)
$path = $manager->save($content, 'my/unsafe:file.txt');
// Results in: my_unsafe_file.txt

// With duplicate filename
$path1 = $manager->save($content1, 'report.pdf');
$path2 = $manager->save($content2, 'report.pdf');
// Results in: report.pdf, report_1.pdf

// With uploaded file
$path = $manager->saveUploadedFile($uploadedFile, 'custom-name.pdf');
// Uses the provided name, sanitized if necessary

// Without filename
$path = $manager->save($content);
// Generates a random filename
bash
php artisan vendor:publish --provider="Vldmir\TempFileManager\TempFileManagerServiceProvider" --tag="config"