Jump to content


This topic is now archived and is closed to further replies.


trying to load images from mySQL

Recommended Posts

How can i load a picture from a mySQL database using php. The picture is uploaded in a table and then dynamically loaded to a php page. It's attribute in the database table is longblob.
[code]while(list($name, $type, $size, $path) = mysql_fetch_array($imgResult))
    $im = open_image($path);

if ($im === false) {
        die ('Unable to open image');

header ('Content-Type: image/jpeg');
echo 'Opened image';

function open_image($path) {
        # JPEG:
        $im = @imagecreatefromjpeg($path);
        if ($im !== false) { return $im; }

        # GIF:
        $im = @imagecreatefromgif($path);
        if ($im !== false) { return $im; }

        # PNG:
        $im = @imagecreatefrompng($path);
        if ($im !== false) { return $im; }

        # GD File:
        $im = @imagecreatefromgd($path);
        if ($im !== false) { return $im; }

        # GD2 File:
        $im = @imagecreatefromgd2($path);
        if ($im !== false) { return $im; }

        # WBMP:
        $im = @imagecreatefromwbmp($path);
        if ($im !== false) { return $im; }

        # XBM:
        $im = @imagecreatefromxbm($path);
        if ($im !== false) { return $im; }

        # XPM:
        $im = @imagecreatefromxpm($path);
        if ($im !== false) { return $im; }

        # Try and load from string:
        $im = @imagecreatefromstring(file_get_contents($path));
        if ($im !== false) { return $im; }

        return false;

Share this post

Link to post
Share on other sites
I am new in PHP, so I don't realy understand what you mean with that script, but when I uploading some images (by FTP functions), i put to MySQL DB only path, where the required picture is located. I also store an orgiginal filename (wallpaper3.jpg) , actual filename (file0018.jpg) which shold be generated by PHP and ID. Than just load info from DB and use <IMG SRC="...">  ;)

Share this post

Link to post
Share on other sites
I agree...

What I do is write a script that uploads the image to a specific directory, renaming it using php's date() function. Then, I insert into a database the new name of the uploaded image, as well as any other data I want to associate with the image (comments, who uploaded the image, the upload date, file size, etc.).

Then, when I want to display the image, I run a MySQL query for the information, and use the image name in the database to display the image, like such:

$imagefile = mysql_result($result,$i,"imagefile");

print "<IMG SRC='../images/$imagefile'>";

very simple....

Share this post

Link to post
Share on other sites
i've tried that, i've made the attribute a varchar and that would point to the file but the server said it could not find the image on the server.

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.