TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   MySQL & Databases (http://www.talkphp.com/mysql-databases/)
-   -   MySQL Fieldtype :: MEDIUMBLOB (http://www.talkphp.com/mysql-databases/2261-mysql-fieldtype-mediumblob.html)

ReSpawN 02-16-2008 07:08 PM

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

Alan @ CIT 02-16-2008 07:19 PM

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

ReSpawN 02-16-2008 07:23 PM

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.

Alan @ CIT 02-16-2008 08:02 PM

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

EyeDentify 02-28-2008 10:12 AM

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? :-D

/EyeDentify

Alan @ CIT 02-28-2008 10:29 AM

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


All times are GMT. The time now is 03:41 AM.

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