Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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
info@net-allies.de

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
    : info@net-allies.de
    :

Sign In or Register to comment.