Home  >  Article  >  Database  >  How to Insert .pdf Files into MySQL BLOBs in PHP?

How to Insert .pdf Files into MySQL BLOBs in PHP?

Linda Hamilton
Linda HamiltonOriginal
2024-10-23 20:09:30569browse

How to Insert .pdf Files into MySQL BLOBs in PHP?

Inserting .pdf Files into MySQL BLOBs Using PHP

Storing files directly in a database can be problematic, particularly for large files such as PDFs. Despite this, it is possible to store .pdf files in MySQL databases as BLOBs (Binary Large Objects) using PHP.

To insert a .pdf file into a BLOB column, you can use the following code snippet:

<code class="php">$sql = "INSERT INTO table (data) VALUES (?)";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("b", file_get_contents("path/to/file.pdf"));
$stmt->execute();</code>

However, it is important to note that storing BLOBs in MySQL is not recommended due to potential performance and maintenance issues. It is generally preferable to store the file on the filesystem and save only a reference (e.g., the file path) in the database. This approach reduces the database size and improves efficiency.

The above is the detailed content of How to Insert .pdf Files into MySQL BLOBs in PHP?. 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