How to store images in a database?
21
I need to store user-uploaded images in my application. I'm considering storing them directly in the database as BLOB data.
What are the best practices for storing images? Should I store them in the database or on the file system? What are the pros and cons of each approach?
1 Answer
0
Image ko database mein store karne ke liye aapko nimnalikhit charanon ka palan karna hoga:
1. Image ko upload karne ke liye form banayein: Aapko ek HTML form banana hoga jismein image upload karne ke liye input field ho.
2. Image ko server par upload karein: Aapko image ko server par upload karne ke liye PHP ya kisi aur programming language ka upyog karna hoga.
3. Image ko database mein store karein: Aapko image ko database mein store karne ke liye SQL query ka upyog karna hoga.
Yeh ek udaharan hai PHP aur MySQL ka upyog karke image ko database mein store karne ka:
**HTML Form**
```html
```
**PHP Code (upload.php)**
```php
```
**MySQL Table**
```sql
CREATE TABLE images (
id INT AUTO_INCREMENT PRIMARY KEY,
image_name VARCHAR(255),
image_data LONGTEXT
);
```
Note: Storing images in database as base64 is not recommended for large scale applications. It's better to store images on file system and save file paths in database.