Skip to content

undecaf/doctrine-file-storage

Repository files navigation

Storing files as BLOBs with Doctrine ORM

PHP applications usually store uploaded files in the server's file system and persist only the file paths in a database. Obtaining a consistent backup of such a dataset may be difficult.

This sample project uses Doctrine ORM to store uploaded files as BLOBs in a database. After all, MySQL supports 4GB BLOBs and PostgreSQL even 4TB BLOBs.

In Doctrine ORM, however, msqli, pdo_mysql and pdo_pgsql do not stream into/from a BLOB but materialize the BLOB in memory in its entirety.

Therefore, the file size for BLOB storage is limited by the PHP memory_limit (128MB by default), and for MySQL also by the max_allowed_packet parameter. In order to stay within these limits, the PHP upload_max_filesize should be set accordingly.

Despite these drawbacks, using BLOBs can be advantageous since this keeps the complete dataset in a single place (the database); this provides for consistent backups and synchronization in a database cluster.

Within this project, a few additional techniques are mentioned that might be useful.

Subjects