Home >Database >Mysql Tutorial >How to Store Files in a MySQL Database Using BLOB Columns?

How to Store Files in a MySQL Database Using BLOB Columns?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-23 16:25:09488browse

How to Store Files in a MySQL Database Using BLOB Columns?

Storing Files in MySQL Database: Column Type and Insert Statement

When inserting a file into a MySQL database, the appropriate column type to use is a BLOB (Binary Large Object) column. BLOB columns store binary data, including files, and come in varying sizes:

  • TINYBLOB: 255 bytes
  • BLOB: 65535 bytes
  • MEDIUMBLOB: 16777215 bytes
  • LONGBLOB: 4294967295 bytes

Note: It's not advisable to store large files in MySQL databases due to performance impacts and increased database size.

Insert Statement:

The INSERT statement for inserting a file into a BLOB column is similar to other data types, but with a slight variation:

INSERT INTO table_name (column_name) VALUES (load_file('/path/to/file.ext'));

Here, /path/to/file.ext represents the absolute path to the file you want to insert.

Example:

To insert a file named document.pdf into a BLOB column named file_data in the document_table, use the following statement:

INSERT INTO document_table (file_data) VALUES (load_file('/home/user/documents/document.pdf'));

The above is the detailed content of How to Store Files in a MySQL Database Using BLOB Columns?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn