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
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Display Modes
Old 02-16-2008, 07:08 PM   #1 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 445
Thanks: 49
ReSpawN is on a distinguished road
Default MySQL Fieldtype :: MEDIUMBLOB

Hey guys,

I've been jerking around (no comment svp) with the MEDIUMBLOB field in MySQL. I've tried uploading a file, ... need I say files but I am not able to return any of them. Not a .exe or .jpg on display.

When retrieving a JPEG file, I tried displaying with with first engaging the header("Content-type: image/jpeg"); but that didn't work.

I asked my guru PHP mate but he didn't knew either or it took to long. Anyways, I just posted it.

Mark
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 02-16-2008, 07:19 PM   #2 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 482
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Using header() should be pretty much all you need to do.

For example:

PHP Code:
// ...Connect to the DB server and select your database...

$result mysql_query("SELECT image FROM myTable WHERE image_id = 1");
header("Content-type: image/jpeg");
echo 
mysql_result($result0); 
Assuming that the image data was inserted correctly into your database originally using something like - file_get_contents($_FILES['userfile']['tmp_name']) - to get the image itself, then it should work fine.

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 02-16-2008, 07:23 PM   #3 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 445
Thanks: 49
ReSpawN is on a distinguished road
Default

I directly uploaded it using phpMyAdmin. I hope that it works as well or else I am doing something horribly wrong. Meaning, it doesn't work.

Can you give me an example of some kind of form using the upload function like you just referenced?

Thanks for the quick reply Alan, love it.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 02-16-2008, 08:02 PM   #4 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 482
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Your form would look something like:

Code:
<form enctype="multipart/form-data" action="upload.php" method="post">
	<input type="hidden" name="MAX_FILE_SIZE" value="5000000" />
	<input name="image[]" type="file" />
	<input type="submit" value="Submit" />
</form>
The important bit being the 'enctype'.

Then in your upload script, you would have something like the following to insert the image to your database:

PHP Code:
$image file_get_contents($_FILES['image']['tmp_name']);
mysql_query("INSERT INTO images (image_data) VALUES ('" $image "')"); 
You'd want to put a few checks in their and use escape_string(), but that's the basics of it.

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 02-28-2008, 10:12 AM   #5 (permalink)
The Contributor
 
EyeDentify's Avatar
 
Join Date: Nov 2007
Location: Sweden
Posts: 91
Thanks: 11
EyeDentify is on a distinguished road
Default

I donīt know if iīm way of base with this statement.

But somewhere i read that you should use base64_encode() on BLOB or binary data before storing it in the database.

And then ofcourse use base64_decode() when you get the data out from the database to display it, or download it or whatever.

Could somebody of our gurus shed som light on this?

/EyeDentify
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
EyeDentify is offline  
Reply With Quote
Old 02-28-2008, 10:29 AM   #6 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 482
Thanks: 51
Alan @ CIT is on a distinguished road
Default

base64'ing binary data is good practice if your data contains things like single quotes that would likey break your query.

It's also handy for database systems that don't support a BLOG type field like older version of Microsoft SQL server. In these cases, you can base64_encode() your file and insert it into a TEXT field instead.

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
The Following User Says Thank You to Alan @ CIT For This Useful Post:
SOCK (02-28-2008)
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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


All times are GMT. The time now is 10:04 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0