Get filename and path after a file is uploaded using afterStateUpdated

I'd like to get the filename and path after a file is uploaded using afterStateUpdated. After the file is uploaded, but before the (create) record is saved, I can see the file in the livewire-tmp folder, but how do I get to it programmatically? (I want to convert a PDF to text at this point) I've searched diligently for code examples on accessing file upload details using $get or $component in an afterStateUpdated handler, but getting nowhere. Seems like something that should be easier to find. Please advise
7 Replies
toeknee
toeknee2w ago
I think you can use livewire?
->afterStateUpdated(fn($get, $livewire) => dd($livewire))
->afterStateUpdated(fn($get, $livewire) => dd($livewire))
But also see: https://www.answeroverflow.com/m/1170008418648592434
How can I get the uploaded file before saving data - Filament
Is there a way to get the uploaded file before saving data to the database in CreateCustomer class that extends CreateRecord
class CreateCustomer extends CreateRecord
{
protected static string $resource = CustomerResource::class;
}
class CreateCustomer extends CreateRecord
{
protected static string $resource = CustomerResource::class;
}
this is the form in resource class ``` Select::make('group_id')->translateLabel() ->options(Grou...
jaredatobe
jaredatobeOP2w ago
$livewire works as suggested. It appears to have roughly the same information available as $component. However, what I'm asking for is how to get to the uploaded filename. dd shows me the filename, buried in the data array, but how do I access that programmatically? The linked answeroverflow answer uses $this->form->getRawState() which throws an error if I try to use it inside the resource class, so I don't even know how to use it.
charlie
charlie2w ago
try with $livewire->data
jaredatobe
jaredatobeOP2w ago
Cool. We're one step closer. How do I programmatically access the 'path' field in the enclosed 'data' array? $livewire->data['existing_policy']->???
No description
charlie
charlie2w ago
the answer is in your screenshot, that's written "array", so something like:
$livewire->data['existing_policy'][0]['path']
$livewire->data['existing_policy'][0]['path']
jaredatobe
jaredatobeOP2w ago
I tried that early on and [0] throws an error ("Undefined array key 0"). I tried numerous variations of brackets and arrows and none of them work. I've tried casting the object as an object and as an array (which is technically possible) in order to access what's inside of it. If I call the array with the GUID $livewire->data['23a1df3...'] I can get to the object directly, but ['path'] doesn't work. So I tried reset() to get at the zeroth element of the associative array, which worked, but again ['path'] doesn't work. So I tried ->getPath() and all I got is the part I already know: C:\xampp\htdocs\<projectname>\storage\app\livewire-tmp, the path up to but not including the filename of the uploaded file. What am I missing? [Update]: Found it. I was missing getFilename(). This works:
$temporaryuploadedfileobject = $livewire->data['existing_policy'];
$filename = reset($temporaryuploadedfileobject)->getFilename();
"EsyyO09GAzYENzZyhSjpfMlJLV28iV-metaQXV0b2RpZGFjdGljaXNtMiAtIFdpa2lwZWRpYS5wZGY=-.pdf"
$temporaryuploadedfileobject = $livewire->data['existing_policy'];
$filename = reset($temporaryuploadedfileobject)->getFilename();
"EsyyO09GAzYENzZyhSjpfMlJLV28iV-metaQXV0b2RpZGFjdGljaXNtMiAtIFdpa2lwZWRpYS5wZGY=-.pdf"
So the one-liner answer is this:
$filename = reset($livewire->data['myfieldname'])->getFilename();
$filename = reset($livewire->data['myfieldname'])->getFilename();
Thanks for the help!
charlie
charlie2w ago
A bit too late but if it can help, I've used this code in a project a few months ago:
->afterStateUpdated(function (Set $set, Get $get) {
$path = $get('path');
if (is_array($path)) {
$path = current($path);
}
if (!empty($path) && $path instanceof TemporaryUploadedFile && empty($get('name'))) {
$name = pathinfo($path->getClientOriginalName(), PATHINFO_FILENAME);
$set('name', $name);
}
$set('preview', $get('path') ?? '');
})
->afterStateUpdated(function (Set $set, Get $get) {
$path = $get('path');
if (is_array($path)) {
$path = current($path);
}
if (!empty($path) && $path instanceof TemporaryUploadedFile && empty($get('name'))) {
$name = pathinfo($path->getClientOriginalName(), PATHINFO_FILENAME);
$set('name', $name);
}
$set('preview', $get('path') ?? '');
})

Did you find this page helpful?