...

View Full Version : Blob



j05hr
10-30-2009, 11:11 AM
How do I take an image from the database and insert it into the website, when I do this the image comes up in loads of letters and symbols.

Thanks,
Josh

abduraooft
10-30-2009, 12:20 PM
Please post your current code.

j05hr
10-30-2009, 12:28 PM
It's the image div line.



<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header.php"); ?>
<div id="content">
<?php
// 3. Perform database query
$result = mysql_query("SELECT * FROM pages", $connection);
if (!$result) {
die("Database query failed: " . mysql_error());
}

// 5. Use returned data
while ($row = mysql_fetch_array($result)) {
?>
<div class="menu-name"> <?php echo $row["menu_name"]." <br />"; ?> </div>
<div class="buying-text"> <?php echo $row["content"]." <br />"; ?> </div>
<div class="image"> <?php echo $row["image"]." <br />"; ?> </div>

<?php
}
?>



</div>
<?php require("includes/footer.php"); ?>


You might also be able to tell me how to write it better, but it worked so that's how I left it. (I'm a real amateur with PHP)

abduraooft
10-30-2009, 12:39 PM
You can't echo an image like this. You need to use <img> tag having the src as another file, which fetches the image based on the reference passed from original file.
Take a look at http://www.phpbuilder.com/board/showpost.php?p=10405191&postcount=3

Phil Jackson
10-30-2009, 12:44 PM
i think you can you file_get_contents and put that data in the db with an id and then use a seperate php page lets say

<img src="imageloader.php?id=1" />

then load that image dynmicaly and echo the data out with an image header() based on its extention.

Phil Jackson
10-30-2009, 12:46 PM
Id have to write it first before i comment further... :-)

j05hr
10-30-2009, 01:03 PM
That would be really helpful Phil, thanks

Phil Jackson
10-30-2009, 01:10 PM
aha! done it.. let me clean it up..

Phil Jackson
10-30-2009, 01:20 PM
okay, made this for if you were storing image from off your server(the net elsewhere, will change to suit if needed).


index.php



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php
if(isset($_POST['imageInsert']))
{
function checkUrl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$header = curl_exec($ch);

if(preg_match("#\s200\sOK#is", $header))
{
return TRUE;
}
else
{
return FALSE;
}
}

$con = mysql_connect("localhost", "root", "");
$db = mysql_select_db("image-store", $con);

if(checkUrl($_POST['url']))
{
$image = mysql_real_escape_string(file_get_contents($_POST['url']));
if(mysql_query("INSERT INTO `images` (image) VALUES ('$image') ") or die (mysql_error()))
{
echo "insert complete";
}
else
{
echo "could not insert error occured";
}
}
else
{
echo "url incorrect";
}
}
elseif(isset($_POST['imageShow']))
{
$src = "imageLoader.php?id=".$_POST['id'];
echo "<h2>Show Image: ID#".$_POST['id']."</h2>\n";
echo "<img src=\"".$src."\" />\n";
}
else
{
echo "<h2>No image Selected To Show</h2>\n";
}
?>

<h2>Upload Image</h2>
<form method="post" action="index.php">
url: <input type="text" name="url" />
<input type="submit" name="imageInsert" />
</form>

<h2>Show image</h2>
<form method="post" action="index.php">
id: <input type="text" name="id" />
<input type="submit" name="imageShow" />
</form>


</body>
</html>


imageLoader:


<?php

if(isset($_GET['id']))
{
$con = mysql_connect("localhost", "root", "");
$db = mysql_select_db("image-store", $con);
$id = $_GET['id'];

$get_query = mysql_query("SELECT * FROM `images` WHERE id = '$id'") or die(mysql_error());
$get_array = mysql_fetch_array($get_query);
$image_contents = $get_array['image'];

header('Content-Type: image/gif');
echo $image_contents;
}

?>


PROBLEMS:

I have only took inter consideration .gif images. can do it for jpeg, gif and png (will do it all in a min)

For the script above to work, created a table called "image-store" with fields 'id' (autocred, primary) and 'image'.

For it to work this all image types you will have to add another field called, lets say 'ext' and i will sort that out.

Phil Jackson
10-30-2009, 01:40 PM
NEW for all images:

php on index page:



<?php
if(isset($_POST['imageInsert']))
{
function checkUrl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$header = curl_exec($ch);

if(preg_match("#\s200\sOK#is", $header))
{
return TRUE;
}
else
{
return FALSE;
}
}

$con = mysql_connect("localhost", "root", "");
$db = mysql_select_db("image-store", $con);
$url = $_POST['url'];

if(preg_match("#((\.jpeg)|(\.jpg)|(\.gif)|(\.png))$#is", $url, $match))
{
if($match[1]==".jpg")
{
$ext = ".jpeg";
}
else
{
$ext = trim($match[1]);
}
$ext = substr($ext, 1);
if(checkUrl($_POST['url']))
{

$image = mysql_real_escape_string(file_get_contents($_POST['url']));
if(mysql_query("INSERT INTO `images` (image, ext) VALUES ('$image', '$ext') ") or die (mysql_error()))
{
echo "insert complete";
}
else
{
echo "could not insert error occured";
}
}
else
{
echo "url incorrect";
}
}
else
{
echo "invalid file type";
}
}
elseif(isset($_POST['imageShow']))
{
$src = "imageLoader.php?id=".$_POST['id'];
echo "<h2>Show Image: ID#".$_POST['id']."</h2>\n";
echo "<img src=\"".$src."\" />\n";
}
else
{
echo "<h2>No image Selected To Show</h2>\n";
}
?>


imageLoader.php



<?php

if(isset($_GET['id']))
{
$con = mysql_connect("localhost", "root", "");
$db = mysql_select_db("image-store", $con);
$id = $_GET['id'];

$get_query = mysql_query("SELECT * FROM `images` WHERE id = '$id'") or die(mysql_error());
$get_array = mysql_fetch_array($get_query);
$image_contents = $get_array['image'];
$image_ext = $get_array['ext'];

header("Content-Type: image/$image_ext");
echo $image_contents;
}

?>

j05hr
10-30-2009, 01:41 PM
It's getting there, it uploads the image but it doesn't show the actual image, do I have to add another field to my database called ext?

Thanks for all your help so far!

Phil Jackson
10-30-2009, 01:43 PM
yeah,

id = init
image = longblob
ext = text

the above code works bangon!...if i do say so myself...

j05hr
10-30-2009, 01:54 PM
yeah,

id = init
image = longblob
ext = text

the above code works bangon!...if i do say so myself...

It's not working on mine, I got all 3 of the fields, when I upload an image it show's the .gif icon but not the actual picture and when I put in a gif url, I get this error,

Fatal error: Call to undefined function curl_init() in C:\wamp\www\image\index.php on line 15

On this line of code,

$ch = curl_init();

Phil Jackson
10-30-2009, 01:56 PM
you, ve not got Curl enabled.... wait there

Phil Jackson
10-30-2009, 01:58 PM
change the function with the curl in to:



function checkUrl($url)
{
if(@file_get_contents($url))
{
return TRUE;
}
else
{
return FALSE;
}
}

j05hr
10-30-2009, 02:08 PM
The picture still isn't coming up when I type the ID

And when I put a url in it's saying Table 'image-store.image' doesn't exist, but in my database the table is called image-store and where the pictures are it's called image, so I'm not sure why it's saying it doesn't exist

Phil Jackson
10-30-2009, 02:12 PM
remove yourt password and username only from your script and post it here plz. Its a connection problem.

j05hr
10-30-2009, 02:15 PM
index.php


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php
if(isset($_POST['imageInsert']))
{
function checkUrl($url)
{
if(@file_get_contents($url))
{
return TRUE;
}
else
{
return FALSE;
}
}

$con = mysql_connect("localhost", "root", "");
$db = mysql_select_db("image-store", $con);

if(checkUrl($_POST['url']))
{
$image = mysql_real_escape_string(file_get_contents($_POST['url']));
if(mysql_query("INSERT INTO `image` (image) VALUES ('$image') ") or die (mysql_error()))
{
echo "insert complete";
}
else
{
echo "could not insert error occured";
}
}
else
{
echo "url incorrect";
}
}
elseif(isset($_POST['imageShow']))
{
$src = "imageLoader.php?id=".$_POST['id'];
echo "<h2>Show Image: ID#".$_POST['id']."</h2>\n";
echo "<img src=\"".$src."\" />\n";
}
else
{
echo "<h2>No image Selected To Show</h2>\n";
}
?>

<h2>Upload Image</h2>
<form method="post" action="index.php">
url: <input type="text" name="url" />
<input type="submit" name="imageInsert" />
</form>

<h2>Show image</h2>
<form method="post" action="index.php">
id: <input type="text" name="id" />
<input type="submit" name="imageShow" />
</form>


</body>
</html>


imageLoader.php


<?php

if(isset($_GET['id']))
{
$con = mysql_connect("localhost", "root", "");
$db = mysql_select_db("image-store", $con);
$id = $_GET['id'];

$get_query = mysql_query("SELECT * FROM `image` WHERE id = '$id'") or die(mysql_error());
$get_array = mysql_fetch_array($get_query);
$image_contents = $get_array['image'];

header('Content-Type: image/gif');
echo $image_contents;
}

?>

Phil Jackson
10-30-2009, 02:17 PM
edit the connection setup to:



$con = mysql_connect("localhost", "root", "") or die (mysql_error());
if(!$db = @mysql_select_db("imxdcxage-store", $con))
{
die("no known db");
}

Phil Jackson
10-30-2009, 02:18 PM
did you remove your details or have you not entered your username and password?

j05hr
10-30-2009, 02:20 PM
It said no known database, I changed the imxdcxage-store back to image-store with the same code and it said Table 'image-store.image' doesn't exist again

j05hr
10-30-2009, 02:28 PM
I removed my password, localhost and root are what I'm using because I'm doing it locally

Phil Jackson
10-30-2009, 02:31 PM
take a look at your database with the table in. Make sure its spelt exactly the same. If no joy there you might want to rename the table say "imagestore" as mysql is wierd and sometimes dont like the names given.

Phil Jackson
10-30-2009, 02:34 PM
did you call the table image or images? i called it images

j05hr
10-30-2009, 02:39 PM
I called it image because that's what you called it originally and I noticed in the code you called it images so just changed it to image, I also just changed it to be called images in the database and changed it to images in the code too, I will give renaming the table a go

Phil Jackson
10-30-2009, 02:41 PM
try this



<?php

if(isset($_POST['imageInsert']))
{
function checkUrl($url)
{
if(@file_get_contents($url))
{
return TRUE;
}
else
{
return FALSE;
}
}

$con = mysql_connect("localhost", "root", "") or die (mysql_error());
if(!$db = @mysql_select_db("imxdcxage-store", $con))
{
die("no known db");
}

if(!@mysql_query("SELECT FROM `images`"))
{
$query = "CREATE TABLE IF NOT EXISTS `images` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`image` longblob NOT NULL,
`ext` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;";
mysql_query($query) or die(mysql_error());
}

$url = $_POST['url'];

if(preg_match("#((\.jpeg)|(\.jpg)|(\.gif)|(\.png))$#is", $url, $match))
{
if($match[1]==".jpg")
{
$ext = ".jpeg";
}
else
{
$ext = trim($match[1]);
}
$ext = substr($ext, 1);
if(checkUrl($_POST['url']))
{

$image = mysql_real_escape_string(file_get_contents($_POST['url']));
if(mysql_query("INSERT INTO `images` (image, ext) VALUES ('$image', '$ext') ") or die (mysql_error()))
{
echo "insert complete";
}
else
{
echo "could not insert error occured";
}
}
else
{
echo "url incorrect";
}
}
else
{
echo "invalid file type";
}
}
elseif(isset($_POST['imageShow']))
{
$src = "imageLoader.php?id=".$_POST['id'];
echo "<h2>Show Image: ID#".$_POST['id']."</h2>\n";
echo "<img src=\"".$src."\" />\n";
}
else
{
echo "<h2>No image Selected To Show</h2>\n";
}

?>



EDIT: Just changed it

j05hr
10-30-2009, 03:04 PM
Did that and it says no image selected to show, I've now uploaded it to live so you can see it www.jasongold.org/index.php (not sure if that will help or not)

Phil Jackson
10-30-2009, 06:06 PM
dont know how this can be going wrong for you. Post your full page here...

Phil Jackson
10-30-2009, 07:29 PM
http://www.actwebdesigns.co.uk/DBimage/index.php

j05hr
11-01-2009, 01:05 PM
I finally got it to work, thanks Phil for all the help, I've thanked you for all the kind help.

I just have one last question, if I wanted to show an image from the database into this code here,



<div class="image"> <?php ?> </div>


How would it work in conjunction with the code you made?

Thanks,
Josh

Phil Jackson
11-01-2009, 09:20 PM
this bit here:



elseif(isset($_POST['imageShow']))
{
$src = "imageLoader.php?id=".$_POST['id'];
echo "<h2>Show Image: ID#".$_POST['id']."</h2>\n";
echo "<img src=\"".$src."\" />\n";
}


You could get rid of the echo and put:


$imgDisplay = "<img src=\"".$src."\" />\n";



then with you post above:



<div class="image"> <?php echo $imgDisplay; ?> </div>

j05hr
11-03-2009, 10:49 AM
I've tried your code and replacing the echo for that line of code but it's saying

Notice: Undefined variable: imgDisplay in C:\wamp\www\image\test.php on line 77. Probably something wrong with what I'm doing



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
if(isset($_POST['imageInsert']))
{
function checkUrl($url)
{
if(@file_get_contents($url))
{
return TRUE;
}
else
{
return FALSE;
}
}


$con = mysql_connect("localhost", "root", "");
$db = mysql_select_db("image-store", $con);
$url = $_POST['url'];

if(preg_match("#((\.jpeg)|(\.jpg)|(\.gif)|(\.png))$#is", $url, $match))
{
if($match[1]==".jpg")
{
$ext = ".jpeg";
}
else
{
$ext = trim($match[1]);
}
$ext = substr($ext, 1);
if(checkUrl($_POST['url']))
{

$image = mysql_real_escape_string(file_get_contents($_POST['url']));
if(mysql_query("INSERT INTO `images` (images, ext) VALUES ('$image', '$ext') ") or die (mysql_error()))
{
echo "insert complete";
}
else
{
echo "could not insert error occured";
}
}
else
{
echo "url incorrect";
}
}
else
{
echo "invalid file type";
}
}
elseif(isset($_POST['imageShow']))
{
$src = "imageLoader.php?id=".$_POST['id'];
$imgDisplay = "<img src=\"".$src."\" />\n";


}
else
{
echo "<h2>No image Selected To Show</h2>\n";
}
?>
</head>
<body>
<div class="image"> <?php echo $imgDisplay; ?> </div>
</body>
</html>


As I'm building a CMS what I would ideally want is a browse feature where they can browse images from their machines and it loads to the database in this page



<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php find_selected_page();?>
<?php include("includes/header.php"); ?>
<div id="content">
<h2> Buying Edit Page</h2>

<?php if (!is_null($sel_subject)) { // subject selected ?>
<h5><?php echo $sel_subject['menu_name']; ?></h5>
<?php } elseif (!is_null($sel_page)) { // page selected ?>

<div class="houseSpecs">
<?php echo $sel_page['content']; ?>
</div>
<?php } else { // nothing selected ?>
<br />
<h5>Add a new page</h5>
<form action="create_subject.php" method="post">
<p>Subject name:
<input type="text" name="menu_name" value="" id="menu_name" />
</p>
<p>Add content:
<textarea name="content" rows="5" cols="70" /></textarea>
</p>
<p>Position:
<select name="position">
<?php
$subject_set = get_all_subjects();
$subject_count = mysql_num_rows($subject_set);
// $subject_count + 1 b/c we are adding a subject
for($count=1; $count <= $subject_count+1; $count++) {
echo "<option value=\"{$count}\">{$count}</option>";
}
?>
</select>
</p>
<p>Visible:
<input type="radio" name="visible" value="0" /> No
&nbsp;
<input type="radio" name="visible" value="1" /> Yes
</p>
<input type="submit" value="Add Subject" />
</form>
<br/>
<a href="content1.php">Cancel</a>
<?php } ?>
<?php


?>
</div>
<?php require("includes/footer.php"); ?>

Phil Jackson
11-03-2009, 10:52 AM
<?php if(isset($imgDisplay){echo $imgDisplay;} ?>

j05hr
11-09-2009, 01:10 PM
This is still giving me loads of problems, I've been trying to put your code into mine but with no success.



function public_navigation($sel_subject = null, $sel_page = null) {
$subject_set = get_all_subjects();
// 5. Use returned data
while ($subject = mysql_fetch_array($subject_set)) {

if(!isset($_GET['page'])) {
echo "<div class=\"menu-name\">"; echo "<a href=\"buying.php?page=" . urlencode($subject["id"]) .
"\">{$subject["menu_name"]}</a>"; echo"</div>";
echo "<div class=\"buying-text\">"; echo "{$subject["content"]}"; echo"</div>"; echo "<div class=\"image\">"; echo"</div>";
}
}
}


I put the <?php if(isset($imgDisplay){echo $imgDisplay;} ?> into the echo "<div class=\"image\">" but when the images just won't load.

Thanks Josh



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum