TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 08-27-2009, 09:17 AM   #1 (permalink)
The Contributor
 
Join Date: Mar 2009
Posts: 49
Thanks: 0
TheOnly92 is on a distinguished road
Default Storing Binary Data in MySQL

I have been trying to storing binary datas like images into the MySQL database. The column type is set to LONGBLOB, but I'm unable to successfully store the file. It seems when I execute the query, it will somehow go corrupted. I have tried using mysql_real_escape_string, addslashes to escape and tried bin2hex to store but all failed. Any suggestion?
TheOnly92 is offline  
Reply With Quote
Old 08-27-2009, 01:18 PM   #2 (permalink)
The Contributor
 
ioan1k's Avatar
 
Join Date: Mar 2009
Location: US
Posts: 76
Thanks: 0
ioan1k is on a distinguished road
Default

How exactly does the query become corrupted? What errors are given

You should be able to store the image using
php Code:
$binaryData = addslashes(fread(fopen($file, "r"), filesize($file)));

When you want to pull the image you will need to use header() to display the binary data.

php Code:
header("Content-type: $type");
header("Content-length: $size");
echo $data;
__________________
My Portfolio - Work - Need freelance Work?
I've been developing 5 years now, and I learn something new everyday
ioan1k is offline  
Reply With Quote
Old 08-27-2009, 01:38 PM   #3 (permalink)
The Contributor
 
Join Date: Mar 2009
Posts: 49
Thanks: 0
TheOnly92 is on a distinguished road
Default

Well, after inserting the data into the database, when I can retrieve from the database and echo it, it appears corrupted. The png image file is unable to be displayed. I'm wondering if it has to do with encoding or something.
TheOnly92 is offline  
Reply With Quote
Old 08-27-2009, 01:42 PM   #4 (permalink)
The Contributor
 
ioan1k's Avatar
 
Join Date: Mar 2009
Location: US
Posts: 76
Thanks: 0
ioan1k is on a distinguished road
Default

Are you send the header information before echoing the binary data?

You will need to send out the following headers.

php Code:
header('Content-type: image/png;');
header('Content-length: $iamgesize);

So you code will appear something like

php Code:
$data = mysql_query('SELECT data, filesize FROM images WHERE imageId = 1');
$data = mysql_fetch_assoc($data);
header('Content-type: image/png;');
header('Content-length: '.$data['filesize'].'');
echo $data['data'];

You cannot print out the data with other html content and must load the images through a php file, for example
html Code:
<div id="user">
    <img src="load-image.php?id=23" alt="Image" />
</div>
__________________
My Portfolio - Work - Need freelance Work?
I've been developing 5 years now, and I learn something new everyday
ioan1k is offline  
Reply With Quote
Old 08-28-2009, 02:12 AM   #5 (permalink)
The Contributor
 
Join Date: Mar 2009
Posts: 49
Thanks: 0
TheOnly92 is on a distinguished road
Default

Yes, I definitely did, it tells me that the image cannot be displayed because it contains errors.
TheOnly92 is offline  
Reply With Quote
Old 08-29-2009, 10:20 AM   #6 (permalink)
The Contributor
 
Join Date: Mar 2009
Posts: 49
Thanks: 0
TheOnly92 is on a distinguished road
Default

I wonder if there is anymore suggestions? I'm getting really stuck here.
TheOnly92 is offline  
Reply With Quote
Old 08-29-2009, 10:27 AM   #7 (permalink)
The Contributor
 
Join Date: Mar 2009
Posts: 49
Thanks: 0
TheOnly92 is on a distinguished road
Default

Oops, I'm sorry, the problem was when the database record was retrieved, my code did an extra unescape which was unnecessary and ruined the data. Sorry for the trouble caused, all is going well now.
TheOnly92 is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Injection and mysql_real_escape_string Durux General 61 01-29-2013 12:20 PM
Storing Session data in $_COOKIE as well shownomercy General 8 08-21-2008 02:00 PM
want to fetch data in excel sheet from my mysql database sharma.9.pooja Absolute Beginners 2 06-24-2008 05:04 AM
what kind of data can go into a mysql database? sarmenhb General 27 03-04-2008 10:51 AM
Log User Data with PHP & MySQL mortisimus Absolute Beginners 6 09-30-2007 05:52 PM


All times are GMT. The time now is 12:20 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design