how can I save an image as a blob?

164 views Asked by At

Such as this :

enter image description here

How do I save images like the format as the above URL ?

1

There are 1 answers

0
Lajos Arpad On

Getting the image

If you need to download the image from the web, you will need to send a request to the URL where the image is located. In PHP you can download an image like this:

file_get_contents($url);

or you could load it from a local file:

file_get_contents($url);

Storing it

Assuming PHP and MySQL you can do it like this:

$sql = "INSERT INTO products(product_name, price, product_image) VALUES (:product_name, :price, :product_image)";

foreach ($products as $product) {
    $stmt = $pdo->prepare($sql);
    $stmt->execute($product);
}

Example taken from https://www.digitalocean.com/community/tutorials/how-to-use-the-mysql-blob-data-type-to-store-images-with-php-on-ubuntu-18-04