Hi,

i can't display the blob pictute in PHP. It only shows a place for the picture in PHP.
Thanks

This is the record in database.

id             name          type                        size          content
-------      ------------- --------------             -------        -------
6	action.JPG	image/pjpeg	2707	BLOB
<?php
$id = 6;
$query = "SELECT name, type, size, content FROM upload WHERE id = $id";

$result = mysql_query($query);
if($row = mysql_fetch_array($result)){
  $imageData = $row['content'];
  $imageType = $row['type'];
}

header("Content-type: $imageType");
echo $imageData;
?>

Recommended Answers

All 7 Replies

try to do this:
lets say the code below is in file
retrive.php

<?php
if(isset($_GET['id'])){
$id =$_GET['id'];
}
$query = "SELECT name, type, size, content FROM upload WHERE id = $id";

$result = mysql_query($query);
if($row = mysql_fetch_array($result)){
  $imageData = $row['content'];
  $imageType = $row['type'];
}

header("Content-type: $imageType");
echo $imageData;
?>

then create another file with name
view.php
there write something like this:
view.php

<?php

echo"<img src=retrieve.php?id='6' width=200 height=150 border=0>";

?>

then to view ur picture just load the view.php

It shows empty box (width=200 height=150 border=0) with a red X in it. No picture

In my code in the view.php, i made a mistake i wrote retrieve.php instead of retrive.php
it should look like this

<?php

echo"<img src=retrive.php?id='6' width=200 height=150 border=0>";

?>

Also in ur code,put the slashes ' '

$query = "SELECT name, type, size, content FROM upload WHERE id ='$id' ";


echo"$imageData";

Hi,
I give you my whole working code. Can you tell me where do i put or call your code?
Thanks

index.html

<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Filename:<input type="file" name="selectedFile"/> 
<input type="submit" name="buttonSubmit" value="Submit" />
</form>
</body>

upload.php

$fileName = $_FILES["file"]["name"];
$fileType = $_FILES["file"]["type"];
$fileSize = $_FILES["file"]["size"];
$tmpName  = $_FILES["file"]["tmp_name"];
	
$s = "INSERT INTO upload (name, size, type, content ) 
	VALUES ('$fileName', '$fileSize', '$fileType', '$content')";	
$sql=mysql_query($s);						

if ($sql) {	echo "OK"; }

in upload.php you have many security problems you need to fix, but i can't see where you are adding the file data to the database. where is $content coming from in your code.

after you fix that/explain to me what you are doing, try to view the image using

<img src="retrieve.php?id=1" width="200" height="150" />

using valid xhtml helps and the ' surrounding the id are not needed.

instead of messing with trying to figure out exactly whats going on, i wrote this example.

<?php

//This is a stand alone example, do include this in anything.

//MySQL credentials (fill these in)
$host = 'localhost';
$user = 'username';
$pass = 'password';
$db   = 'dbname';

$con = mysql_connect( $host,$user,$pass ) or die( 'Error: Could not connect' );
mysql_select_db( $db ) or die( 'Error: Unable to select database' );

$table =<<<SQL
CREATE TABLE IF NOT EXISTS `uploaded_images` (
	`id` INT NOT NULL AUTO_INCREMENT,
	`name` VARCHAR(30) NOT NULL,
	`type` VARCHAR(30) NOT NULL,
	`size` INT NOT NULL,
	`content` MEDIUMBLOB NOT NULL,
	PRIMARY KEY(`id`)
);
SQL;
$query = mysql_query( $table,$con );

$case = $_GET['case'];

switch( $case ) {
	case "upload":
		if ( isset( $_POST['submit'] ) && count( $_FILES ) > 0 ) {
			if ( isset( $_FILES['image']['name'] ) && $_FILES['image']['name'] !== '' ) {
				$fileName = basename( $_FILES['image']['name'] );
				$fileType = $_FILES['image']['type'];
				$tempFile = $_FILES['image']['tmp_name'];
				$fileSize = filesize( $tempFile );
				$open = fopen( $tempFile,'r' );
				$data = fread( $open,$fileSize );
				fclose( $open );
				$sql   = "INSERT INTO `uploaded_images` (`name`,`type`,`size`,`content`) VALUES ('{$fileName}','{$fileType}','{$fileSize}','{$data}')";
				$query = mysql_query( $sql,$con );
			}
		}
		$url = $_SERVER['PHP_SELF'];
		echo <<<HTML
<form action="$url?case=upload" method="post" enctype="multipart/form-data">
<input type="file" name="image" /><br />
<input type="submit" name="submit" value="Upload" />
</form>
HTML;
	break;
	case "view":
		if ( isset( $_GET['id'] ) ) {
			$id = mysql_real_escape_string( $_GET['id'] );
			if ( is_numeric( $id ) ) {
				$sql   = "SELECT `type`,`content` FROM `uploaded_images` WHERE `id` = {$id}";
				$query = mysql_query( $sql,$con );
				$total = mysql_num_rows( $query );
				if ( $total == 1 ) {
					list( $type,$data ) = mysql_fetch_row( $query );
					header( "Content-type: {$type}" );
					echo $data;
					exit;
				}
			}
		}
	break;
}

?>

its untested but should work. just upload and try it. let me know if there are errors. to upload something go to filename.php?case=upload and it view an image go to filename.php?case=view&id={id of image}

i'll try it and let you know. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.