Files
2026-06-03 12:11:28 +03:30

24 lines
741 B
PHP

<?php
namespace App\Traits;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Str;
trait StoresUploads
{
/**
* Store an uploaded file under a random name while preserving its real
* extension (from the original filename). Laravel's default store() derives
* the extension from the sniffed MIME type, which yields ".bin" for files
* that sniff as application/octet-stream (e.g. some valid .mp3 files).
*/
protected function storeUpload(UploadedFile $file, string $folder, string $disk = 'public'): string
{
$ext = $file->getClientOriginalExtension()
?: ($file->guessExtension() ?: 'bin');
return $file->storeAs($folder, Str::random(40) . '.' . $ext, $disk);
}
}