Save file contents of PDF to database longblob field
I’m trying to save the contents of a PDF file into a longblob field of a mariadb database.
I understand that this should be stored in the filesystem, however, the requirements call for the files to be stored in the database.
Inside my form I have a FileUpload that accepts the pdf file type:
DocumentResource.php
Not sure if the afterStateUpdated is the correct method.
I attempted to open the file for reading, however, it fails to open the stream. The error reads no such file or directory, however, that is where the file appears to be getting saved. I tried passing the TemporaryFileUpload and tried using file_get_contents but couldn't get either to work.
Is this a lifecycle hook issue, or am I off base on how this should work? I am new to Filament. I have accomplished this before (saving contents to db) in several projects many years ago with success using $_FILES in php.
createDocument.php
mutateFormDataBeforeCreate the culprit?
Solution:Jump to solution
I was able to get this to work using the handleRecordCreation method by adding it to the CreateDocument.php file. Everything else pretty much stayed the same. Thanks again, Lara, for pushing me in the right direction!
9 Replies
bumping this back up in the hopes of getting some insight. There may not be a Filament way to simply store the contents of a file in a database, although I would think this would be simple (but beyond my understanding!). I cannot figure out how to even access the contents of the temporary file and store it in a database. I guess it's probably best to run the code outside, but thought it could have been cleaner keeping it inside the resource as there have been numerous questions related to this, but not a single definitive answer that I can tell.
what is the value of the $file['path']
is it URL or just path?
maybe
Thanks for the reply.
The value of fopen($file['path']) is :
fopen(/opt/homebrew/var/www/app/storage/app/livewire-tmp/NmX4w5tj4zv1B8CltveUIJkuBZHTgk-metaNS5wZGY=-.pdf): Failed to open stream: No such file or directory
let me try the file_get_contents suggestion.
file_get_contents leaves me with a similar error. (file_get_contents(/storage//opt/homebrew/var/www/app/storage/app/livewire-tmp/tL45nNQkqouNzeO0jKSpejOUZ4CpmS-metaNS5wZGY=-.pdf): Failed to open stream: No such file or directory). It is almost as if the file doesn't save to the temp folder until after uploading on the page then hitting submit. If that's the case, then I have no idea how to get the contents. In the old days of procedural programming and using globals, I'd just use fopen( $_Files['document']['tmp_name'] , 'rb')' then read the file "fread" and it would work. Gotta have access to the file and path, however, to make that work and I can't see how to get access to the contents of the file. I must be missing something.
try it with storage_path
file_get_contents(storage_path($file['path']))
same error, failed to open stream, no such file or directory. I'm not sure the file is saved into the temporary directory at the time the mutateFormDataBeforeCreate runs. But again, I haven't much experience with Filament, so not sure.
check the folder livewire-tmp if the file exist?
I think the file will be deleted after the state saved
other aproche is to let filament save the file, then afterCreate hook get it by URL and update it to DB, then delete it
livewire-tmp exists and was full of temp files. I manually removed them. When attempting to save the files now, no new files are created in the livewire-tmp directory, however, they are getting created in the storage/app/public folder. I would prefer to handle the save at the time of upload instead of creating a separate process to lookup the file by URL then persist to the db. Does
state
not have access to the file contents? state
has access to just about everything else, but I don't see a method to grab the contents of the file. I would have thought this would be much easier than it has turned out to be. I do appreciate your efforts in helping.Solution
I was able to get this to work using the handleRecordCreation method by adding it to the CreateDocument.php file. Everything else pretty much stayed the same. Thanks again, Lara, for pushing me in the right direction!
Not sure how to mark this as solved, but thanks again.