FileUpload dynamic directory issue

I've build a Fileupload Helper like so to be reusable across resources:
class FileUploadHelper
{
/**
* Create a standardized file upload component
*
* @param bool $required
*/
public static function createFileUpload(
string $name,
string $label,
string $directoryName,
string $prefix,
string $dbColumn,
string $subdirectory,
array $options): FileUpload
{
$upload = FileUpload::make($name)
->label(__($label))
->directory(
fn (Get $get): string => $directoryName . '/'
. ($get('prefix') ?? $prefix . '#' . str_pad((string)$get($dbColumn), 4, '0', STR_PAD_LEFT))
. '/'
. $subdirectory
)
->openable();

// Options
if (isset($options['hiddenLabel']) ?? false) {
$upload->hiddenLabel();
}

// Some more ...

return $upload;
}
}
class FileUploadHelper
{
/**
* Create a standardized file upload component
*
* @param bool $required
*/
public static function createFileUpload(
string $name,
string $label,
string $directoryName,
string $prefix,
string $dbColumn,
string $subdirectory,
array $options): FileUpload
{
$upload = FileUpload::make($name)
->label(__($label))
->directory(
fn (Get $get): string => $directoryName . '/'
. ($get('prefix') ?? $prefix . '#' . str_pad((string)$get($dbColumn), 4, '0', STR_PAD_LEFT))
. '/'
. $subdirectory
)
->openable();

// Options
if (isset($options['hiddenLabel']) ?? false) {
$upload->hiddenLabel();
}

// Some more ...

return $upload;
}
}
it's working like charm, here is how it is used in the resource:
FileUploadHelper::createFileUpload(
name: 'accident_images',
label: 'Accident Images',
directoryName: 'Accidents',
prefix: 'MVA',
dbColumn: 'ac_number',
subdirectory: 'Accident Images',
options: [
'required' => true,
'image' => true,
'multiple' => true,
'maxFiles' => 20,
'imagePreviewHeight' => 250,
'panelLayout' => 'grid',
'hint' => "Make sure the files are in any image format (jpg, jpeg, png, webp, gif)",
'hintColor' => 'primary',
]
),
FileUploadHelper::createFileUpload(
name: 'accident_images',
label: 'Accident Images',
directoryName: 'Accidents',
prefix: 'MVA',
dbColumn: 'ac_number',
subdirectory: 'Accident Images',
options: [
'required' => true,
'image' => true,
'multiple' => true,
'maxFiles' => 20,
'imagePreviewHeight' => 250,
'panelLayout' => 'grid',
'hint' => "Make sure the files are in any image format (jpg, jpeg, png, webp, gif)",
'hintColor' => 'primary',
]
),
In short, each accident has a reference number (MVA#0000). The files are uploaded correctly to the dynamic folders. but when I access the edit/view page of the record, the images are not showing, and when trying to access them though url I get 404 not found (see attached). May you please assist me with solving/tracing this? Thank you
No description
3 Replies
toeknee
toekneeβ€’3d ago
inspect the apge for where it's trying to load them from. It could be a miss-match of the directory
SoraKeyheart
SoraKeyheartOPβ€’3d ago
It turned out to be the #. I shouldn't use hashtags in the directory as it breaks the URL.
toeknee
toekneeβ€’3d ago
That makes sense! ensure you are using Str::slug for the dir names / files to be safe πŸ™‚

Did you find this page helpful?