Hi again, much questions today!

So i'm having an Form for adding pictures with som details to a database. Everythings works well but some attributes i wish the user didnt need to enter, such as ,type (jpeg, gif), size, date.

Dani AI

Generated

Practical server-side approach for (and following up on ): when you keep only the image path in the database, compute and store metadata from the file itself. For local files use secure server-side functions to get MIME type, file size and timestamps; if the image contains EXIF data you can prefer the camera DateTimeOriginal for the picture date.

<?php
$path = '/path/to/image.jpg'; // validate and sanitize this input first

if (is_readable($path)) {
    // MIME type (preferred: fileinfo)
    if (function_exists('finfo_open')) {
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        $mime  = finfo_file($finfo, $path);
        finfo_close($finfo);
    } else {
        $info = getimagesize($path);
        $mime = isset($info['mime']) ? $info['mime'] : null;
    }

    $size     = filesize($path);          // bytes
    $modified = filemtime($path);         // unix time

    $exifDate = null;
    if (function_exists('exif_read_data')) {
        $exif = @exif_read_data($path);
        if (!empty($exif['DateTimeOriginal'])) {
            $exifDate = str_replace(':', '-', substr($exif['DateTimeOriginal'], 0, 10))
                       . ' ' . substr($exif['DateTimeOriginal'], 11);
        }
    }

    $dateToStore = $exifDate ? $exifDate : date('Y-m-d H:i:s', $modified);
    // store $mime, $size, $dateToStore in DB
}
?>

Notes and cautions: prefer finfo over file extension or client-sent headers; EXIF is JPEG/TIFF only and requires the PHP exif extension; PHP file functions work on URLs only if allow_url_fopen is enabled — for remote images download them or fetch headers via cURL instead. Always validate paths, prevent directory traversal, and check file type before trusting or displaying uploads. See the PHP docs for details on getimagesize, finfo_file and exif_read_data.

Recommended Answers

All 2 Replies

Much questions???
Where?

Another project and another tread..

Forget to say i'm not storing the image in the db only the image location, and what i want to do is with the imagelocation get the details of the image.

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.