I want to put image uploading in my page and i searched all over the net for some ways on doing this and its seems like almost all of the ways states that i should save it in the database itself not i directories. Will the image retain its properties from its original file upon retrieving a saved image? Can anyone help me how to do this? tanx..

Dani AI

Generated

Short answer to : if you save and later retrieve the original file bytes, the image keeps its pixels and embedded metadata (EXIF) — whether you store those bytes as a file on disk or as a BLOB in MySQL. The only time "properties" change is when you explicitly re-encode, resize, or strip metadata during upload or delivery.

Echoing and , the usual recommendation for web apps is to store images on disk or in an object store (S3, etc.) and keep the path/metadata in the database. That approach lets the webserver serve static files efficiently, keeps database backups smaller, and plays nicely with CDNs and caching. Storing images in the DB is valid for small, transactional needs, but it increases DB size and backup/restore costs — see MySQL BLOB considerations (MySQL BLOB types).

Practical, secure workflow (high level):

  • Validate uploads (MIME with finfo, file size limits, getimagesize to confirm image).
  • Give files safe, unique names (random or hashed) and store them in an uploads folder outside direct public write paths.
  • Store path + MIME + dimensions + size in the DB; generate web-optimized thumbnails on upload.
  • Serve static files directly where possible; if you must gate access, stream with correct Content-Type headers.

Tiny PHP example (store to disk; store path/metadata in DB):

$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime  = $finfo->file($_FILES['image']['tmp_name']);
$allowed = ['image/jpeg'=>'.jpg','image/png'=>'.png'];
if (isset($allowed[$mime])) {
  $name = bin2hex(random_bytes(12)).$allowed[$mime];
  move_uploaded_file($_FILES['image']['tmp_name'], __DIR__.'/uploads/'.$name);
  // insert $name, $mime, filesize(), getimagesize() into DB
}

Extra notes for : files on disk do not inherently use more space than DB storage — BLOBs add DB overhead and make dumps larger. For production, use object storage + CDN, validate content, limit sizes, and back up both files and DB together so references remain consistent. For PHP upload details see the official docs (file upload basics).

Recommended Answers

All 3 Replies

never put an image into the database itself. just upload them into a directory and put the path to file in the database.

It is always better to store the images on disk instead of database. File operations are always faster than database. Saving the images on disk will reduce your database size which will give you comfort when moving database or mainting database.

Don't save images in database unless you have some special requirements.

But if you still interested to same images in database then here is a small post to in database.

never put an image into the database itself. just upload them into a directory and put the path to file in the database.

but in folder the image take more space than database so far i know; so will it be not wise to use database ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.