Displaying the image from database - image gets truncated

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sandip.bhoi
Forum Newbie
Posts: 1
Joined: Thu Aug 30, 2007 3:12 am

Displaying the image from database - image gets truncated

Post by sandip.bhoi »

feyd | This topic has been split from another: viewtopic.php?t=47552


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am displaying the image stored in database.  The problem what i am facing is, the image displayed is truncated and only the partial image is shown. Then if i makes request to another page(php page), then instead executing the php page
the browser displays the download page pop up. On downloading the php script page, the page if opened in text editor, shows some unreadable text (probably the truncated image content) and then proper php script. Following is the snapshot of page downloaded

Code: Select all

Q@Q@Q@Q@Q@Q@Q@Q@Q@Q@ÿÙHTTP/1.1 200 OK
Date: Thu, 30 Aug 2007 07:11:09 GMT
Server: Apache/2.2.4 (Win32) PHP/5.2.3
X-Powered-By: PHP/5.2.3
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 523
Keep-Alive: timeout=5, max=98
Connection: Keep-Alive
Content-Type: text/html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<title> Welcome to the Network</title>
		<link rel="stylesheet" type="text/css" href="css/winxp.blue.css"/>

</head>
<body>
<div class="screenLayout">

<div class="headerContainer">
	<div class="pageHeader"> 				
	</div>
</div>

<div align="right">
<a href="logout.php"> Logout</a>
</div><h3>Successfully logged Out.</h3>
<h2> <a href="index.php"> Login again</a></h2>
</body>
</html>

The page which fetches the image from database is

Code: Select all

<?php
//getImage.php
function __autoload($class_name) {
    require_once $class_name . '.php';
}
?>
<?php	
	$photo_id=$_REQUEST["photo_id"];
?>
<?php
	session_start();
	$authorized = false;
	$loggedUser = $_SESSION["LOGGED_USER"];
	if ($loggedUser == null) {
		header('Location: index.php') ;
	} else {	
		$authorized = true;		
	}
	$photo_id=$_REQUEST["photo_id"];
?>
<?php
if ($authorized) {
	require_once("dbConnection.php");
	$con = getConnection();
	//flush();
	$selectPhotoQuery = "select * from photos where photo_id=".$photo_id;
	$resultSet = mysql_query($selectPhotoQuery) or die("Error: coud not select image");
	$recordSelected =  mysql_numrows($resultSet);
	if ( $recordSelected == 1) {
		$row = mysql_fetch_array($resultSet);
		$fileSize = $row["file_size"];
		$fileType = $row["file_type"];
		$fileImageContent = $row["photo_blob"];			
	} else {
		//default image
		$defaultFileName = "img/default.jpg";
		$fp      = fopen($defaultFileName, 'r');
		$fileSize = filesize($defaultFileName);
		$fileImageContent = fread($fp, $fileSize);
		$fileType = "image/jpeg";
		//$fileImageContent = addslashes($fileImageContent);							
		fclose($fp);
	}
	mysql_close($con);
	header("Content-type: ".$fileType); 
	header("Content-length: ".$fileSize); 	
	echo $fileImageContent;
	exit();
	} else {
		header("Location: index.php"); 
	}
?>
And the image is displayed as

Code: Select all

<?php
echo "\n<img src='getImage.php?photo_id=".$loggedUser->getUserPhotoId()."' width='100' height='100' border='1'>";	
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


[color=darkgreen][b]feyd[/b] | This topic has been split from another: http://forums.devnetwork.net/viewtopic.php?t=47552[/color]
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

@The-Master: Mixing frameset HTML and layout HTML is terrible. Your code won't work in any browser that sticks to standards.

@sandip.bhoi: You don't have an img tag in your HTML so it's not likely to display any images.
Post Reply