Home > Backend Development > PHP Tutorial > How to Properly Store Images (Blobs) in MySQL Using PHP?

How to Properly Store Images (Blobs) in MySQL Using PHP?

Linda Hamilton
Release: 2024-11-30 13:47:11
Original
1048 people have browsed it

How to Properly Store Images (Blobs) in MySQL Using PHP?

Storing Blobs in MySQL Databases Using PHP

When attempting to store an image in a MySQL database, you may encounter issues if your query fails to insert the image. This article will provide solutions to this problem.

Problem:

In the following query, file_get_contents($tmp_image) is treated as a string rather than a function call:

$sql = "INSERT INTO ImageStore(ImageId,Image) 
VALUES('$this->image_id','file_get_contents($tmp_image)')";
Copy after login

Solution 1:

Jump out of the string and perform explicit concatenation:

$sql = "INSERT INTO ImageStore(ImageId,Image) 
VALUES('$this->image_id','" . file_get_contents($tmp_image) . "')";
Copy after login

Solution 2:

Escape binary data to prevent invalid queries:

$sql = "INSERT INTO ImageStore(ImageId,Image) 
VALUES('$this->image_id','" . mysql_escape_string(file_get_contents($tmp_image)) . "')";
Copy after login

Solution 3:

Minimize database size by storing images in a file system or by using cloud storage services.

The above is the detailed content of How to Properly Store Images (Blobs) in MySQL Using PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template