1

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

1 Answer 1

1

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;
5
  • Actually my requirement is as follows, Customers are allowed to upload customer specific documents and that gets saved to their account. Right now the customer will upload a .docx file and that gets saved in the file system. Moving forward I want to save it in the core_file_storage table. I saw a class Mage_Core_Model_File_Storage_Database in app/code/Mage/Core/Model/File/Storage/Database.php which could be used to save the file. But I am very new to magento so i am not able to move forward. Oct 28, 2014 at 22:56
  • the core_file_storage table has file id, content(blob), upload_time, filename, directory_id, directory as columns Oct 28, 2014 at 23:01
  • ah i see.. core_file_storage table is used by Magento for storing media files in that table, when you have enabled it at Config>Advanced>System>Storage config for media. Make your custom module with DB.
    – Kalpesh
    Oct 28, 2014 at 23:29
  • May I know the reason why you want to keep files in DB? That's not good practice and can cause performance issue.
    – Kalpesh
    Oct 28, 2014 at 23:30
  • It is a decision made by the software architect. I am just a developer Oct 29, 2014 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.