This commit is contained in:
2026-06-03 12:11:28 +03:30
parent bae1a2acb0
commit 0cb4954579
6 changed files with 51 additions and 15 deletions
+4 -1
View File
@@ -4,6 +4,7 @@
use App\Models\Image;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
trait HandlesImageUpload
{
@@ -18,7 +19,9 @@ protected function uploadedImageId(Request $request, string $field = 'image'): ?
return null;
}
$path = $request->file($field)->store('images', 'public');
$file = $request->file($field);
$ext = $file->getClientOriginalExtension() ?: ($file->guessExtension() ?: 'jpg');
$path = $file->storeAs('images', Str::random(40) . '.' . $ext, 'public');
return Image::create([
'user_id' => auth()->id(),
+23
View File
@@ -0,0 +1,23 @@
<?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);
}
}