filament-tinyeditor
filament-tinyeditor copied to clipboard
Keep the Original Filename When Uploading Images
I am trying to upload an image via Filament TinyEditor and I would like to retain the original filename of the image when it is uploaded. However, the image is being uploaded with a random filename.
Could you please provide guidance on how to keep the original filename when uploading a new image through the editor?
Did you find any solution for it?
I just figured it out, if you want to keep the original file name add your own ->saveUploadedFileAttachmentsUsing() callback. I used this, this example creates a slug of the original filename with an underscore as the seperator.
<?php
TinyEditor::make('content')
->label('Content')
->saveUploadedFileAttachmentsUsing(
function (TemporaryUploadedFile $file) {
$originalName = $file->getClientOriginalName();
$pathInfo = pathinfo($originalName);
$extension = $pathInfo['extension'] ?? '';
$filename = $pathInfo['filename'] ?? $originalName;
$sluggedName = Str::slug($filename, '_') . '.' . $extension;
return $file->storeAs('uploads', $sluggedName, 'public');
}
),