PHP code example of christopheraseidl / laravel-auto-filer

1. Go to this page and download the library: Download christopheraseidl/laravel-auto-filer 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/ */

    

christopheraseidl / laravel-auto-filer example snippets


use christopheraseidl\AutoFiler\HasAutoFiles;

class Product extends Model
{
    use HasAutoFiles;

    protected $fillable = [
        'images',
        'current_report',
        'description',
    ];

    protected function casts(): array
    {
        return [
            'images' => 'array',
        ];
    }

    // Option 1: Using a property
    protected $file = [
        'images' => 'images',
        'current_report' => 'documents'
    ];

    // Option 2: Using a method
    public function files(): array
    {
        return [
            'images' => 'images',
            'current_report' => 'documents'
        ];
    }

    // Property for rich text fields
    protected $richText = [
        'description' => 'content',
    ];

    // Or using a method
    public function richTextFields(): array
    {
        return [
            'description' => 'content',
        ];
    }
}

// Creating a new product with file uploads
$product = new Product([
    'name' => 'Awesome Product',
    'images' => [
        'uploads/temp/image1.jpg',
        'uploads/temp/image2.jpg',
    ],
    'current_report' => 'uploads/temp/current_report.pdf',
]);

$product->save();

// Files are now moved to:
// - products/1/images/image1.jpg (with thumbnail: image1-thumb.jpg if enabled)
// - products/1/images/image2.jpg (with thumbnail: image2-thumb.jpg if enabled)
// - products/1/documents/current_report.pdf

// Updating files
$product->images = ['uploads/temp/new-image.jpg'];
$product->save();
// Old images and their generated thumbnails are deleted, 
// new one is moved to products/1/images/new-image.jpg
// and a new thumbnail is automatically created if enabled

// Deleting the model
$product->delete();
// All associated files and their thumbnails are automatically deleted

return [
    // Storage configuration
    'disk' => 'public',
    'temp_directory' => 'uploads/temp',
    
    // Queue configuration
    'queue_connection' => null,
    'queue' => null,
    'broadcast_channels' => null,
    
    // File validation
    'max_size' => 5120, // 5MB in KB
    'mimes' => [
        'image/jpeg',
        'image/png',
        'image/svg+xml',
        'application/pdf',
        'application/msword',
        'text/plain',
        'video/mp4',
        'audio/mpeg',
        // ... more MIME types
    ],
    'extensions' => [
        'jpg', 'jpeg', 'png', 'svg', 'ico',
        'pdf', 'doc', 'docx', 'odt', 'rtf',
        'txt', 'md', 'mp4', 'mp3',
    ],
    
    // Thumbnail generation
    'thumbnails' => [
        'enabled' => false,
        'width' => 400,
        'height' => null,
        'suffix' => '-thumb',
        'quality' => 85,
    ],
    
    // Cleanup configuration
    'cleanup' => [
        'enabled' => false,
        'dry_run' => true,
        'threshold_hours' => 24,
        'schedule' => 'daily',
    ],
    
    // Retry and throttling
    'maximum_file_operation_retries' => 3,
    'retry_wait_seconds' => 1,
    'throttle_exception_attempts' => 10,
    'throttle_exception_period' => 10, // minutes
];
bash
php artisan vendor:publish --tag="laravel-auto-filer-config"