Link to home
Start Free TrialLog in
Avatar of error77
error77

asked on

upload image to blob field

Hi all,

I am tring to upload an image to a blob field and just created a field on type blob, thinking that was it...but I get all types of different things over the net..so now I have this:

<form id="form" method="post" enctype="multipart/form-data">
<input type="file" name="thumbnail" />

...then to save:

$thumbnail = $_REQUEST['thumbnail'];

//Not sure about this:
$fileName = $_FILES['thumbnail']['name'];
 $tmpName = $_FILES['thumbnail']['tmp_name'];
 $fileSize = $_FILES['thumbnail']['size'];
 $fileType = $_FILES['thumbnail']['type'];

and finally:

$sql = "insert into users(firstname,lastname,phone,email,thumbnail) values('$firstname','$lastname','$phone','$email','$thumbnail')";

I'm I doing this wrong? I'm confused with this.

Thanks
Avatar of DrDamnit
DrDamnit
Flag of United States of America image

Why do you want to upload an image into a blob field? This is not a best practice.

It is more efficient and easier to upload the picture, and save it into a directory under a unique file name (such as a hash of the timestamp) and then store the file path in the database.
Avatar of error77
error77

ASKER

I've decided to do it this way because getting uploads to work another way is a total nightmare so after month's of fails I've decided to go this route.
Why was it a fail? What was the problem?

Trust me, this is not a great way to go. The Blob field is generally used to store binary data, which is fine, but the problem will arise when you put load on the server and it has to create images on the fly from the blob data and then destroy them. Plus, you needlessly bloat the database.
Avatar of error77

ASKER

Last one I tried was uploadify and the upload worked but I needed to save the uploaded filename full path and couldn't find a way to get this.

I find it easy to put file uploader scripts together as they are but when I try to just add file upload to my Existing code it never works in my case.

In my current case I have a fully working form that saves to mysql database and I just want to add the <input type="file" /> field to it and the add the $myImage = $_REQUEST['myImage']; and the save it to the database with the other fields:

For example:

$sql = "insert into users(firstname,lastname,phone,email,myImage) values('$firstname','$lastname','$phone','$email','$myImage')";

but I just cannot get this working :o/
I use jUpload for this:
http://jupload.sourceforge.net/

It allows single and multiple uploads, and POSTs whatever infor you need about individual files to a file of your choice. (The sample POSTs to dummy_upload.php if I remember correctly).

It is a little bit of a pain to get working sometimes because the documentation is not great, but I can help you get this working.
If you are going to save your image content into blob field, you have to read file content from physical file, not from $_REQUEST.

I agree with DrDamnit, it is not the best practice.
Avatar of error77

ASKER

Checked it out and it's java ... really don't like java :o/
Yes.. the applet is java, but it is self contained. It takes care of the upload for you, and hands the file processing off to a php file.
Download the example, and put your changes in the upload_dummy.php file that goes with it. You can add other form inputs and variables to the applet because it accepts HTML inputs. You don't have to know anything about java.

Just make sure the places you're writing to are writable.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial