?
Solved

Displaying Images from MYSQL using php

Posted on 2003-02-27
12
Medium Priority
?
263 Views
Last Modified: 2006-11-17
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.
0
Comment
Question by:mratcliffe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 7

Expert Comment

by:Big_Red_Dog
ID: 8033217
0
 

Expert Comment

by:ezicoder
ID: 8033341
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";
  }
0
 
LVL 1

Expert Comment

by:KC_Speedball
ID: 8033629
Big_Red_Dog's solution/hintis great... he solved it for me ;)
0
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 

Author Comment

by:mratcliffe
ID: 8033849
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.
0
 
LVL 7

Expert Comment

by:Big_Red_Dog
ID: 8035159
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.
?>


0
 

Author Comment

by:mratcliffe
ID: 8035824
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.
0
 
LVL 7

Accepted Solution

by:
Big_Red_Dog earned 200 total points
ID: 8036616
First of all, the client side, you need to use "?" and not "$", http://localhost/doit.php?ID=1234

Server side, you printed out some text instead of just an image.  It you are going to put both the text and the image on the page together, then you need to generate HTML (you can put just text or just a picture out without HTML, but with both, you'll need to put out HTML.  It can be REALLY simple and you wouldn't use readfile() anymore.  I left everything in a pure php solution, but some of this would be easier outside of the PHP tags.

<?php
echo "<HTML><HEAD></HEAD><BODY>";
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 "<P>";
echo "The Cause for this alarm is:" . $cause;
echo "<P>";
echo "The picture is: ";
echo "<BR>";
echo "<img alt='The error's image' src='" . $row['file_loc'] . "'>";
echo "</BODY></HTML>";
?>

0
 

Author Comment

by:mratcliffe
ID: 8041577
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.
0
 

Author Comment

by:mratcliffe
ID: 8060328
Thank you much. I got it to work
0
 
LVL 7

Expert Comment

by:Big_Red_Dog
ID: 8060421
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
0
 

Author Comment

by:mratcliffe
ID: 8063788
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.
0
 
LVL 7

Expert Comment

by:Big_Red_Dog
ID: 8063903
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
0

Featured Post

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question