displaying images. | Member | | Join Date: Feb 2008
Posts: 53
| | |
i used a code from a website that allows you to display images. however everything works fine from storing the image to the database but it does not display the image.
the following code is the one i have used. Storing the images:
[PHP]<HTML>
<HEAD><TITLE>Store binary data into SQL Database</TITLE></HEAD>
<BODY>
<?php
if (isset($_REQUEST['submit'])) {
mysql_connect("localhost","root","");
mysql_select_db("binary_data");
$data = addslashes(fread(fopen($_FILES['form_data']['tmp_name'], "r"), $_FILES['form_data']['size']));
$result= mysql_query("INSERT INTO binary_data (description,bin_data,filename,filesize,filetype) ".
"VALUES ('".$form_description."','".$data."','".$_FILES['form_data']['name']."','".$_FILES['form_data']['size']."','".$_FILES['form_data']['type']."')");
$id= mysql_insert_id();
print "<p>This file has the following Database ID: <b>$id</b>";
mysql_close();
} else {
?>
<form method="post" action="store.php" enctype="multipart/form-data">
File Description:<br>
<input type="text" name="form_description" size="40">
<INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="1000000">
<br>File to upload/store in database:<br>
<input type="file" name="form_data">
<p><input type="submit" name="submit" value="submit">
</form>
<?php
}
?>[/PHP] Getting the image from database :
[PHP]<?php
if($id) {
mysql_connect("localhost","root","");
mysql_select_db("binary_data");
$query = "select bin_data,filetype from table where id='$id'";
$result = mysql_query($query);
$data = mysql_result($result,0,"bin_data");
$type = mysql_result($result,0,"filetype");
header( "Content-type: $type");
header( "Content-type: image/pjpeg");
echo $data;
};
?>[/PHP] displaying the image:
[PHP]<html>
<head>
<title>images retrieved from database</title>
</head>
<body>
<img src="getdata.php?id=3">
</body>
</html>[/PHP]
if someone can help i would greatly appreciate it.
thanks
ashraf
|  | Expert | | Join Date: Jan 2008 Location: Bath, UK
Posts: 1,609
| | | re: displaying images.
Open getdata.php?id=3 in your browser.
See what error it gives.
It should be $_GET['id'] instead of $id |  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,949
| | | re: displaying images. Quote:
Originally Posted by hsriat Open getdata.php?id=3 in your browser.
See what error it gives.
It should be $_GET['id'] instead of $id More over, should be:
[php]
$id = $_GET['id']; # do some cleaning of this.
[/php]
You should, also, sanitize that input; it could be malicious.
Regards.
| | Member | | Join Date: Feb 2008
Posts: 53
| | | re: displaying images.
thanks for ur reply guys
but still no luck i'v tried both ways. i'm new to php and i have an assignment to hand in soon. anymore suggestion.
do u think storing the url to the images would be easier?
thanks in advance.
ashraf
|  | Expert | | Join Date: Jan 2008 Location: Bath, UK
Posts: 1,609
| | | re: displaying images. Quote:
Originally Posted by ashraf02 thanks for ur reply guys
but still no luck i'v tried both ways. i'm new to php and i have an assignment to hand in soon. anymore suggestion.
do u think storing the url to the images would be easier?
thanks in advance.
ashraf What error do you get when you open getdata.php?id=3 in your browser?
| | Member | | Join Date: Feb 2008
Posts: 53
| | | re: displaying images.
i get no error just a blank screen
| | Member | | Join Date: Feb 2008
Posts: 53
| | | re: displaying images. Quote:
Originally Posted by hsriat What error do you get when you open getdata.php?id=3 in your browser? sorry i only put in getdata.php not getdata.php?id=3
this is the following erros i get on the page.
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in C:\wamp\www\getdata.php on line 13
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in C:\wamp\www\getdata.php on line 14
Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\getdata.php:13) in C:\wamp\www\getdata.php on line 16
Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\getdata.php:13) in C:\wamp\www\getdata.php on line 17
| | Member | | Join Date: Feb 2008
Posts: 53
| | | re: displaying images.
i've sorted the problem out. i looked at the errors and figured out wat was wrong in mysql_query it said select from table instead of the actual table name.
thanks anyway i wouldn't have found that out if u didn't tell me to look at the errors on that particular script. nice one "hsriat" and "markusn00b"
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,949
| | | re: displaying images.
Try using a while loop to assign the data:
[php]
$_res = mysql_query("SELECT .... ");
while($_rows = mysql_fetch_array($_res))
{
$_data = $_rows['bin_data'];
$_file = $_rows['filetype'];
}
[/PHP]
Regards.
EDIT: Neglect this then.
Glad you've sorted it!
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: displaying images.
Do not post duplicate threads in this forum! I does not help you in any way. It just annoys people.
The duplicate thread has been removed.
moderator
| | Member | | Join Date: Feb 2008
Posts: 53
| | | re: displaying images.
i didn't duplicate the thread. next time b4 u delete a post read the code and maybe compare the two codes and check. this code was for BLOB files and the other was for linking url files.
|  | Expert | | Join Date: Jan 2008 Location: Bath, UK
Posts: 1,609
| | | re: displaying images. Quote:
Originally Posted by ashraf02 i didn't duplicate the thread. next time b4 u delete a post read the code and maybe compare the two codes and check. this code was for BLOB files and the other was for linking url files. In the code you provided in the other thread ( removed one), view source of your page in a browser (recommended FF), and see what is the problem with your img tag's src attribute.
Check if the referred source actually exists.
| | Member | | Join Date: Feb 2008
Posts: 53
| | | re: displaying images.
thanx "HSRIAT" but i have sorted the problem out i did wat u said and it seemed that i forgot to put a forward slash in the code thanks any way much appreciated.
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,949
| | | re: displaying images. Quote:
Originally Posted by ashraf02 i didn't duplicate the thread. next time b4 u delete a post read the code and maybe compare the two codes and check. this code was for BLOB files and the other was for linking url files. You DID duplicate the thread.
I read the new thread, fully, and it was in no-way different from this thread.
Even it a question was partly linked to this thread, you should keep it on the same thread.
Regards,
|  | Expert | | Join Date: Jan 2008 Location: Bath, UK
Posts: 1,609
| | | re: displaying images. Quote:
Originally Posted by markusn00b You DID duplicate the thread.
I read the new thread, fully, and it was in no-way different from this thread.
Even it a question was partly linked to this thread, you should keep it on the same thread.
Regards, It was a bit different. In that one, he was saving the name of the image in the db, and in this one, image is saved in db.
Though I'm sorry for argument.
:)
Harpreet
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,501 network members.
|