Picture upload in MySQL Database - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Picture upload in MySQL Database

SouldrinkerSouldrinker Posts: 143Member

Here some function to save an uploaded file as blob to MySQL-Database:

[code]
//Retreiving Information on the temporary File PHP has generated

$file_dest = $file_name;

// Open it and read it out to $myBLOB

$name = "tmp";

while(file_exists("$name.tmp"))
{
$name .= "x";
}

copy($file_dest, "$name.tmp");

$myBLOB = base64_encode(join("", file("$name.tmp")));

// Save it to the DataBase

include("../include/config.php");

$com = dblogon($cnt[server], $cnt[user], $cnt[pass], $cnt[db_name]);

$sql = "INSERT INTO pictures VALUES ('', '$myBLOB', '$file_name')";

mysql_query($sql, $com);

// Re-Retreiving informations on the id of the new entry

$sql = "SELECT picture_id FROM pictures WHERE picture_name = '$file_name'";
$res = mysql_query($sql, $com);

$row = mysql_fetch_array($res);

$blob_id= $row[picture_id];

// close connection and return ID of generated entry

mysql_close($com);

return $blob_id;
[/code]

And here my try to read this out again:

[code]

include("include/config.php");

$com = dblogon($cnt[server], $cnt[user], $cnt[pass], $cnt[db_name]);

$sql = "SELECT picture_content FROM pictures WHERE picture_id='$id'";

$res = mysql_query($sql, $com);

$row = mysql_fetch_array($res);

$blob = base64_decode($row[picture_content]);

mysql_close($com);

Header("Content-Type: image/gif
");
Header("Content-Transfer-Encoding: base64

");

echo $blob;
[/code]

The "../include/config.php3" has got some database-connection variables that are a secret ;)

Now the script works fine with all files, except images (The content-type header is for testing only). If I upload some Image and try to display it via the show-routine there is no picture found, or better said MSIE tells me the picture was -1 Byte of Size.

Would be glad if anyone has a clue what my Error is.

best regards,

sebastian mohrenstecher
executive secretary
net::allies

www.net-allies.de
[email protected]

Comments

  • Alex_EldersonAlex_Elderson Posts: 104Member
    Try this:

    [code]

    @ob_end_clean();
    @ignore_user_abort(false);

    if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')){
    // IE cannot download from sessions without a cache
    @header('Cache-Control: public');
    }
    @header("Content-type: image/gif");
    @header("Content-length: ".(string)(strlen($blob)));

    echo $blob;

    [/code]


    :
    : Here some function to save an uploaded file as blob to MySQL-Database:
    :
    : [code]
    : //Retreiving Information on the temporary File PHP has generated
    :
    : $file_dest = $file_name;
    :
    : // Open it and read it out to $myBLOB
    :
    : $name = "tmp";
    :
    : while(file_exists("$name.tmp"))
    : {
    : $name .= "x";
    : }
    :
    : copy($file_dest, "$name.tmp");
    :
    : $myBLOB = base64_encode(join("", file("$name.tmp")));
    :
    : // Save it to the DataBase
    :
    : include("../include/config.php");
    :
    : $com = dblogon($cnt[server], $cnt[user], $cnt[pass], $cnt[db_name]);
    :
    : $sql = "INSERT INTO pictures VALUES ('', '$myBLOB', '$file_name')";
    :
    : mysql_query($sql, $com);
    :
    : // Re-Retreiving informations on the id of the new entry
    :
    : $sql = "SELECT picture_id FROM pictures WHERE picture_name = '$file_name'";
    : $res = mysql_query($sql, $com);
    :
    : $row = mysql_fetch_array($res);
    :
    : $blob_id= $row[picture_id];
    :
    : // close connection and return ID of generated entry
    :
    : mysql_close($com);
    :
    : return $blob_id;
    : [/code]
    :
    : And here my try to read this out again:
    :
    : [code]
    :
    : include("include/config.php");
    :
    : $com = dblogon($cnt[server], $cnt[user], $cnt[pass], $cnt[db_name]);
    :
    : $sql = "SELECT picture_content FROM pictures WHERE picture_id='$id'";
    :
    : $res = mysql_query($sql, $com);
    :
    : $row = mysql_fetch_array($res);
    :
    : $blob = base64_decode($row[picture_content]);
    :
    : mysql_close($com);
    :
    : Header("Content-Type: image/gif
    ");
    : Header("Content-Transfer-Encoding: base64

    ");
    :
    : echo $blob;
    : [/code]
    :
    : The "../include/config.php3" has got some database-connection variables that are a secret ;)
    :
    : Now the script works fine with all files, except images (The content-type header is for testing only). If I upload some Image and try to display it via the show-routine there is no picture found, or better said MSIE tells me the picture was -1 Byte of Size.
    :
    : Would be glad if anyone has a clue what my Error is.
    :
    : best regards,
    :
    : sebastian mohrenstecher
    : executive secretary
    : net::allies
    :
    : www.net-allies.de
    : [email protected]
    :

Sign In or Register to comment.