Question

I need to save an uploaded file to the database preferrably in the table core_file_storage as a blob and retrieve it later. I want this connected to a customer. Currently the files are being saved in the file system and this needs to be discontinued.

Solved: I used the model mage/core/file/Model/storage/database.php methods to write into the table core_file_storage

Was it helpful?

Solution

Not sure why you are discontinuing storing files in filesystem, storing in DB is not recommended as your DB will get huge quickly.

What is the table schema? You can save blob files by getting file content using file_get_contents PHP function.

Try something like this, after replacing below with your real data:

Mage::getModel('core/file_storage')
->setCustomerId($custId)
->setYourFile(file_get_contents('/path/to/file.ext'))
->save();

And then fetching:

Mage::getModel('core/file_storage')->load($custId);
$blobFile = $cust->getYourFile();

//And to display in frontend... if an image:

header("Content-type: image/jpg"); //Send the content Type here.
echo $blobFile;
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top