Jump to content

Recommended Posts

i can upload an image and insert the information i want about it into a database, the problem is, im trying to insert the image itself into the database as a medium blob. im not sure what im doing wrong because when i echo the query and try selecting the image to view it after it has been inserted into the database, a bunch of funny looking symbols appear. is this the binary form of the image? how can i get around this? some of my code is below

 

$content = addslashes(fread(fopen($file,"r"), filesize($filetmpName)));

$query = "INSERT INTO $Images (Image_Name, Size, Image, Username, Type, URL) VALUES ('$filename', '$filesize', '$content', '$_SESSION[username]', '$filetype', '$URL')";

        $result= MYSQL_QUERY($query);

echo $query;

 

You would call the image as so in your page: < img src="http://yoursite.comimage.php?id=6"> (without the space :-)

 

In your image.php script, you send the image like this:

        // get the image from the db
        $sql = "SELECT image FROM testblob WHERE image_id=0";

        // the result of the query
        $result = mysql_query("$sql") or die("Invalid query: " . mysql_error());

        // set the header for the image
        header("Content-type: image/jpeg");
        echo mysql_result($result, 0);

 

Source: http://www.phpriot.com/articles/images-in-mysql/8

Are you sending out a proper header?

 

I don't think you're understanding how to use the image script.

 

You have a webpage that you want to place a database image in. We'll say your script is called image.php.

 

In your webpage, you display the image as < img src="/image.php?id=5">

Then you should see an image like it's supposed to be.

<?php 
header('Content-type: image/gif'); 
readfile("image.gif"); //sends binary information -- the image
?>

 

Above is the barebones to displaying an image with PHP.

 

Notice you need to send an HTTP header that tells the browser what kind of information it's receiving. That's why you're getting all the gobbly-gook. The browser doesn't know it's an image.

 

Search image mime types for the correct mime type to use.

 

Notice that all your code is missing is the header.

        //test viewing an image from the database
$query = "SELECT Image, Type FROM $Images WHERE ImageId='11'";
$result= MYSQL_QUERY($query);
$data = @ mysql_fetch_array($result);
echo $data["Image"];

just want to clarify, im only saving the image into the database not my server in case that is what the readfile function is for. regarding the header... if i put it in the middle of my code, it says the header information has already been sent, if i put it at the beginning of my code before everything with single quotes, it says internet explorer cannot open my web page, if i put the header in double quotes, it shows just an x where the image should display and a ÿ (i forgot to add in the codebin under $data... i put $type = @MYSQL_RESULT($result,0, "Type"); to read the type and added $type as the content-type in the header which should be correct. so.... no funny looking symbols.... just a red x where the image should be, any more ideas? again, thanks for stickin with me in this problem i'm having.

OK. This is really not that complex. The uploading is the complex part.

 

Your image.php script: (yes, ten lines is all this simple script needs)

<?php
$imageid = $_GET['file'];
// *****Insert MySQL connection details here*****
$query = "SELECT Image, Type FROM $Images WHERE ImageId='$imageid'";
$result= MYSQL_QUERY($query);
$data = @ mysql_fetch_array($result);
header("Content-type: image/jpeg");
echo $data["Image"];
?>

 

Your page with the image on it:

<img src="http://www.******.com/imageupload.php?file=11">

 

BTW, what type of image have you uploaded? Is it a JPG, GIF, PNG??? That will affect the header in the image script.

 

I think that since you're getting the red box with the X in it, you should re-upload the image to the database.

for future viewers, i made the mistake of not taking the source of the image from another page... the whole time i was trying to integrate everything into one page... make an image.php and imageupload.php and you'll be set.

 

i wanna thank pkSML for helping me through this the entire time.

 

PHP Freaks..... recommend it to anyone..... GREAT forum.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.