think tank forum

technology » db -> fs

lucas's avatar
17 years ago
link
lucas
i ❤ demo
so i have been storing images in the database for a client's site. i decided this was dumb, so i pulled them out. 400 rows (1600 images) and the script finished within a second! :D

<?php

require "inc_common.php";

$path = "imglib/";
$suffix = ".jpg";

$sql = "SELECT * FROM bl_picture";
if (!$result = mysql_query($sql)) showerror();
while ($picture = mysql_fetch_array($result)) {

    if(!$handle = fopen($path.$picture["picture_id"]."_800x600".$suffix, 'x')) {
        die("fatal error: could not open file");
    };

    if(!fwrite($handle, $picture["800x600"])) {
        die("fatal error: could not write to file");
    };

    if(!fclose($handle)) {
        die("fatal error: could not close file");
    };

    if(!$handle = fopen($path.$picture["picture_id"]."_480x360".$suffix, 'x')) {
        die("fatal error: could not open file");
    };

    if(!fwrite($handle, $picture["480x360"])) {
        die("fatal error: could not write to file");
    };

    if(!fclose($handle)) {
        die("fatal error: could not close file");
    };
    if(!$handle = fopen($path.$picture["picture_id"]."_160x120".$suffix, 'x')) {
        die("fatal error: could not open file");
    };

    if(!fwrite($handle, $picture["160x120"])) {
        die("fatal error: could not write to file");
    };

    if(!fclose($handle)) {
        die("fatal error: could not close file");
    };
    if(!$handle = fopen($path.$picture["picture_id"]."_120x90".$suffix, 'x')) {
        die("fatal error: could not open file");
    };

    if(!fwrite($handle, $picture["120x90"])) {
        die("fatal error: could not write to file");
    };

    if(!fclose($handle)) {
        die("fatal error: could not close file");
    };

    echo $picture["picture_id"]." complete.<br />\n";

};

?>
lucas's avatar
17 years ago
link
lucas
i ❤ demo
p.s. i had 30MB of pictures in the database. :D
lucas's avatar
15 years ago
link
lucas
i ❤ demo
this still amazes me.
maple's avatar
15 years ago
link
maple
i like large datasets
someone gave me a db where they stored images in the database as well. more like 600MB+ of images though. ridiculous.... wrote a conversion script in python (utilizing a tiny bit of django). was posting it here but i dont break lines at 80 chars. :/