Retrieving an image stored in database

Chris Blair 05 Dec, 2010
Dear All,
I have reached one of those headbanging moments and if I wasn't scared of doing real damage to my head I would go ahead :-)

I have a form which works. I have a CC which also works (to a degree). And, I have my iPhone set up so that I can add/edit/upload to the MySQL table. The problem is this...

When I create a new record and upload a file using the web browser all is fine. The data is entered into the correct fields, the uploaded file is sent to the correct folder, the subsequent link to that image is generated and inserted into a field for display using CC. You can see here:-

http://www.o-dwyertransport.com/index.php?option=com_chronoconnectivity&connectionname=pod_retrieval

Now, when I create a new record and populate the fields via my iPhone all the data is inserted into the correct fields but the problem is the image. This is uploaded directly to the db and not saved into the upload location - there is no way around this and I am not unhappy with that, and if I can achieve that from both platforms I would be even happier. If you look at the link above you will see that the last field on record 23 is full of characters which when clicked produce a null image. I expected this as there is no actual file going into the upload folder. My question is this...How can I get CC to return the image and create a link to it for downloading/viewing in litebox (as in the other links) rather than the characters you see now. I messed around with BLOB, VARCHAR etc and the result were

BLOB displays the characters
VARCHAR displays the link as in all the other fields i.e. viewpod but of course the resulting click produces null.

Can anyone give me a kickstart in the right direction on this one?

TIA & Regards

Chris Blair
GreyHead 05 Dec, 2010
Hi Chris,

I've no idea what the answer to your question is but perhaps you can find a way to detect whether the page is being viewed from a browser or an ipad and show the data correspondingly?

See here for some detection code.

Bob

PS Joomla! has browser sniffer code built in but it probably pre-dates the iPad.
Chris Blair 06 Dec, 2010
Hi Bob,
I have decided to go down a different route. The iPhone is incapable of file upload in the same way a computer / browser is, so the image files I upload from the phone get inserted directly into the db in its raw state. I have decided to to mirror this when files are uploaded from within the website / computer.

I have put together some code, which when run outside of Chronoforms does exactly what I need it to i.e. load the image file directly into the db in its raw state. However, when I try to use the exact same code in Chronoforms I get nothing - not strictly true, I get the uid, name, the other data from the form and even the image file name but not the raw image data. Would you be willing to look at the code for me and see where I am going wrong because for the life of me I can't see it. More frustrating as I know the code works outside of Chronoforms.

I have tried the following code in every place imaginable :-) I have also got enctype correctly defined in the Form Tag Attachment. The form / code is not pretty - I like to get the functionality nailed before making it look prettier so apologies if you find it confusing.

<?php
// connect to mysql server
$link = mysql_connect('localhost', 'root', 'root');
if (!$link) {
    die('Not connected : ' . mysql_error());
}
// connect to database server
$db_selected = mysql_select_db('odt_db', $link);
if (!$db_selected) {
    die ('Database error : ' . mysql_error());
}

$maxFileSize = "1000000";
$image_array = array("image/jpeg","image/jpg","image/gif","image/bmp","image/pjpeg","image/png");
$fileType = $_FILES['file_6']['type']; 
$msg = ''";

if(@$_POST['Submit'])
{
if (in_array($fileType, $image_array)) 
{
if(is_uploaded_file($_FILES['file_6']['tmp_name'])) 
{
if($_FILES['file_6']['size'] < $maxFileSize)
{
$imageData =addslashes (file_get_contents($_FILES['file_6']['tmp_name']));
$sql = "INSERT INTO cjb_chronoforms_Annaveigh (image) VALUES ('$imageData')";
mysql_query($sql) or die(mysql_error());
$msg = " Data successfully uploaded";
}
else 
{
$msg = ' Error :  File size exceeds the maximum limit ';
}
}
}
else
{
$msg = 'Error: Not a valid image ';
}
}
?>


TIA & Regards

Chris
GreyHead 06 Dec, 2010
Hi Chris,

You need to have an 'image' column in the database table, then refresh the DB Connection to make sure that ChronoForms knows about it (see below).

Then the following code in the OnSubmit Before box should do the trick. (You need to have Send Emails set to 'yes' on the General tab for this box to execute.)
<?php
$uploads =& CFUploads::getInstance($MyForm->formrow->id);
$file_path = $uploads->attachments['file_6'];
$imageData = addslashes(file_get_contents(file_path));
JRequest::setVar('image', $imageData);
?>

Bob

You need to refresh the DB Connection after any changes to database column names. In the Form Editor click the DB Connection tab and set the Connection to 'No'. Click the 'Apply icon in the toolbar to save the form, open the DB Connection tab, set the Connection back to 'Yes' and re-save the form. This will refresh the copy of the table information that ChronoForms uses.
Chris Blair 06 Dec, 2010
Hi Bob,
I was studiously comparing your code with mine and had one of those 'I think I have just reinvented the wheel' moments :-) There's a lot to be said for experience. I shall give your suggestion a whirl tonight and see how we go with that - the very least it will do is reduce the strain on my server :-)

Thanks again for the response - When I make it back into work the beers are on me!

Warm Regards

Chris
Chris Blair 09 Dec, 2010
Hi Bob,
Two things...I have just realised that the title of this thread is incorrect - it should be Storing..... Confusion reigns :-)

Now, I tried your code

<?php
$uploads =& CFUploads::getInstance($MyForm->formrow->id);
$file_path = $uploads->attachments['file_6'];
$imageData = addslashes(file_get_contents(file_path));
JRequest::setVar('image', $imageData);
?>


And unfortunately it doesn't work. I stripped this right back and created a test table that accepts the file upload and nothing else. What gets entered into the table field is the file name, not the content as you would imagine when using file_get_contents. I am going off to try some other things but if you have any input on this I would, as always, be grateful.

TIA & Regards

Chris
GreyHead 10 Dec, 2010
Hi Chris,

There's a typo in my code: this line was missing a $
$imageData = addslashes(file_get_contents($file_path));
When that is replaced it successfully uploads something into a column type of BLOB from a ChronoForm.

The code is in the OnSubmit Before box with Send Emails set to Yes on the form general tab.

Bob
Chris Blair 11 Dec, 2010
Bob,
Apologies for the other post - I was reaching despair at that point. The code you posted here worked after I ammended the typo and inserted the missing $. Many thanks for that. The bit I struggled with after that was extracting the mime type, name, size from file_6. I tried to mirror your code to get the contents to extract the mime type etc but after going round the houses I gave up. Ordinarily I wouldn't give up but I have been locked into this for a while now and it was just getting too much so I decided to go back to that which I knew worked and gave me all the info from the uploaded file. Sadly, I had problems with that (as per the fatal error post). Sorry I didn't get back to you sooner, sorry I backtracked, and sorry that I didn't persevere for longer.

I greatly appreciate the help and support - I would have not gotten as far as I have???? had that not been there. Many Thanks!!!!

Regards

Chris
GreyHead 12 Dec, 2010
Hi Chris,

It's not a problem. I had just forgotten if I had actually posted or not. I knew that I'd tested the code . . . sometimes the intention to post gets confused with the actual posting :-(

Bob
shirzuis 04 Dec, 2011
Hi there,

i have few question and i hope you guys can help me out.
i want to save the image itself in DB. and then retrieve it directly from DB. so the main point is only registered users can see their own image.
The image should not be viewed from url if user is not loged in.

or is there any other way i can do this?

kind regards
GreyHead 05 Dec, 2011
Hi shirzuis,

There are two different questions here.

First, we (at least Chris and I) never got the image to save and relaod successfully from the database. This usually isn't a problem, you can save the image in a folder and put the name (and folder) in the database table.

Second, you need to add a check in the CheonoConnectivity WHERE box to get the user's ID and make sure that they only see their data. There are many posts here about that part.

Bob
shirzuis 05 Dec, 2011
Hi Bob,
Thanks for your reply.
I search alot in the forum and other websites to get the image safe in DB. and i think i was able to save it in DB but couldn't able to retrieve it back from db, because it was retrieving it from a folder, in this case which i didn't want.

secondly thanks again for your hint, actually i managed to get the user's data when they are login. it works fine. (thanks for your tutorial(book))

As i am creating this form to get clients data & copy of their ID as an image, and i don't want someone should link it to other website or use it in e.t.c ways.
for example: if i send your avatar link to my friend, he/she can easily view your avatar from any browser without login into site;
http://www.chronoengine.com/forums/download/file.php?avatar=270_1249400200.jpg
I hope you get what i mean?

Can i make it somehow so it should be encrypted, so only Joomla login user can view it?

I have one more question. is it possible to encrypt all the data in form? if possible then i get SSL?

Sorry to ask more, i am not an advance developer. i am only beginner.
GreyHead 07 Dec, 2011
Hi shirzuis,

I'm not sure about this question. I've never succeeded in getting an image back out of the database.

It may be possible to set up your site to stop images being displayed if the domain is differetn for yours but I don't know exactly hwo to do this.

Bob
This topic is locked and no more replies can be posted.