Link to home
Start Free TrialLog in
Avatar of mratcliffe
mratcliffe

asked on

Displaying Images from MYSQL using php

Please help, I really need a good simple example.

My setup:
Database is "images"
Table is "pictures"

fields are:
id is int
description is varchar
pic is blob

I need to get and display the image in a pure php page (.php) extension. I call this page from a seperate program:

run iexplorer.exe http://localhost/alarm.php$id=1

I don't want to reference a url in the database that points to the image. I want the image stored in the blob. ( different reasons) I am a newbie so please have mercy.
Avatar of Big_Red_Dog
Big_Red_Dog

I assume you have picture in your MySql table...

You read that picture into buffer ex.:

$dPictureData = " ... jpeg content ..."

Your header should look like:

HTTP/1.1 200 OK
Server: XXXX
Date: Wed, 26 Feb 2003 09:11:04 GMT
Content-Type: image/gif
Accept-Ranges: bytes
Last-Modified: Thu, 20 Feb 2003 10:55:24 GMT
Content-Length: 745

// ok here is function how to download file use upper header in that function to show image. $sFileData can be your picture buffer
function DownloadFile_PutFile($sFileName, $sFileExtension, $sFileData)
  {
    $iFileLen = strlen($sFileData);    
    $datum = date("D, d M Y H:i:s")." GMT";
    header("Content-Type: application/$sFileExtension");
    header("Accept-Ranges: bytes");
    header("Last-Modified: $datum");    
    header("Expires: $datum");
    header("Content-Disposition: inline; filename=$sFileName.$sFileExtension");
    header("Pragma: no-cache");
    //header("Cache-Control: no-cache, must-revalidate");
    header("Cache-control: private");
    header("Content-Length: $iFileLen\n");    
    echo "$sFileData";
  }
Big_Red_Dog's solution/hintis great... he solved it for me ;)
Avatar of mratcliffe

ASKER

Hi, I had read both of these before submitting a question.
The articles gave me good insight... but I still don't get it.

The senario is: A plant operator pics on a graphics button (seperate graphics package i.e. Foxboro I/A) This loads a variable about control point (a analog input).

The button also fires up iexplorer and passes the point variable i.e. ID to the .php page.

I use that variable in a where clause to query the db and bring up the logic picture to display to the operator. I want to do this in one .php file with no html files involved.
In pseudo-code:

Let's assume when the button is pushed to pass the ID, it calls up doit.php and the ID is passed in as a query_string.  So the button's link is something like this: http://www.mysite.com/doit.php?ID=12345.  Of course if you are talking about a localized control machine, then you have to have a server with PHP configured into it running (probably apache) and you call it this way: http://127.0.0.1/this_controls_directory_under_htdocs/doit.php?ID=12345

--- doit.php ---
<?php
$sql = "SELECT * FROM mytable WHERE ID = " . $_GET['ID'];
$result = mysql_query( $sql );  // Do your error chaching, add your own "or die" etc. here...

// Assume we've stored the name of the file as column file_loc
$row=mysql_fetch_array($result);
$result = readfile( $row['file_loc'] );

// And now your picture is presented in your browser, no HTML, as long as it is a format the browser can display.
?>


Yes I do have apache/php all running fine. Here is my do it.php file that gets the alarm text information. Now I just need to add to my select statement the field that stored the image and display it along with the text.

Client side http://localhost/doit.php$ID=1234

Server side: doit.php

<?php
echo( "Alarm Message for Tag, $ID" );
$db = mysql_connect("localhost","admin","admin");
mysql_select_db("aam",$db);
$sql = "select * from alarm_messages where tagname = '$ID'";
$result = mysql_query($sql,$db);
$row = mysql_fetch_array($result);
$cause = $row["cause"];
echo "The Cause for this alarm is:$cause";
?>

As you can see I am not real fancy. I really don't know
about some of your sytax i.e.   " . $_GET['ID']; or ($row['file_loc'] the but I am going to try.

If my picture name is pic_1.jpg then I'll use:
 
$result = readfile( $row['pic_1.jpg'] );

I'm not sure what you mean about the format part of the browser not supporting the image. I guess that is what the header section is supposed to take care of? I'm getting close and I really appreciate the response. I wish I had a real good book full of examples. I have been looking hard.
ASKER CERTIFIED SOLUTION
Avatar of Big_Red_Dog
Big_Red_Dog

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
Ok, I am close. Sorry about the $ instead of ?, my typo in the comment.

If I did use a mix of html and php (now assuming my file has a .html extension) how would I call up the html page from the client and pass it the ID value?

Thanks again.  I think your explainations are going to help a lot of people.
Thank you much. I got it to work
Glad I could help.  I am sorry I missed your additional question on 2/28.  EE notifications have been flakey lately and I didn't get an EMail that you had posted an additional comment.

Since you already got it to work I will leave a comment for anyone else who trips on thie Q with the answer to your 2/28 comment/question:

There are a couple of ways to do this.  The first is to rename the file to have a .php extension instead of .html with the ?ID=1234 query_string.  The second is to change your .htaccess file to recognize .html as a file type that must be processed by PHP.  In this case as well, you use the query_string ?ID=1234
Thanks for the extra tidbit. When I was first trying it I had done the opposit. I took the .php file and renamed it .html....I didn't get far. Where is the .htaccess file located. I searched all through the apache and php directory.
It is something you create yourself from scratch and put in the directory where your web pages are.  There are a lot of things you can control with the .htaccess file (if you are running on Wintel, you might use win.htaccess).  You can control access to directories.  You can control what error pages are displayed with the different errors, like 404.  You can control what file extensions are processed by which preprocessor, such as in this case.

See this for a good description of the htaccess file: http://apache-server.com/tutorials/ATusing-htaccess.html