Link to home
Start Free TrialLog in
Avatar of shareea
shareea

asked on

Display image from mysql database path

What's easiest way to display a jpeg from a database path?

Attached is code used to store images into directory and info/path into database.
<?
// image directory
$uploadDir = ( $_SERVER['DOCUMENT_ROOT'].'/gallery/gallery1/' );
 
 
if(isset($_POST['upload']))
{
	$fileName = $_FILES['userfile']['name'];
	$tmpName  = $_FILES['userfile']['tmp_name'];
	$fileSize = $_FILES['userfile']['size'];
	$fileType = $_FILES['userfile']['type'];
 
    // get the file extension first
	$ext      = substr(strrchr($fileName, "."), 1); 
	
	// generate the random file name
	$randName = md5(rand() * time());
	
	// unique file name for the upload file
    $filePath = $uploadDir . $randName . '.' . $ext;
 
    // move the files to the specified directory with error reporting
    $result    = move_uploaded_file($tmpName, $filePath);
	if (!$result) {
		echo "Error uploading file";
		exit;
	}
 
  	if (file_exists(( $_SERVER['DOCUMENT_ROOT'].'/gallery/gallery1/' ) . $_FILES['userfile']['tmp_name']))
      {
      echo $_FILES['userfile']['tmp_name'] . ' already exists. ';
      }
 
	
	include_once( $_SERVER['DOCUMENT_ROOT'].'/includes/php/databaseConfig.php' );
 
    if(!get_magic_quotes_gpc())
    {
        $fileName  = addslashes($fileName);
        $filePath  = addslashes($filePath);
    }  
 
	$query = "INSERT INTO gallery1 (name, size, type, path ) ".
			 "VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";
 
    mysql_query($query) or die('Error, query failed : ' . mysql_error());                    
 
    mysql_close();
    
    echo $fileName." uploaded successfully.<br />";
}		
?>
<html>
<head>
<title>Upload File To MySQL Database</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.box {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
	border: 1px solid #000000;
}
-->
</style>
</head>
 
<body>
<form action="" method="post" enctype="multipart/form-data" name="uploadform">
  <table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
    <tr> 
      <td width="246"><input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="userfile" type="file" class="box" id="userfile">
		 </td>
      <td width="80"><input name="upload" type="submit" class="box" id="upload" value="  Upload  "></td>
    </tr>
  </table>
</form>
</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of SandmanNet
SandmanNet

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shareea
shareea

ASKER

Getting a broken image link icon.

Can you explain the image source portion of tag - particularly , "", ?
//view.php
 
<?
include_once( $_SERVER['DOCUMENT_ROOT'].'/includes/php/databaseConfig.php' ); 
 
$q=mysql_query("select * from gallery1") or print mysql_error();
while ($r=mysql_fetch_assoc($q)){
        list($width, $height, $type, $attr) = getimagesize($r["path"]);
        print "<img src='/" . str_replace($_SERVER["DOCUMENT_ROOT"],"", $r["path"]) . "' $attr border='0' />";
}
?>

Open in new window

Avatar of shareea

ASKER

Here's sample of path in db:
/data/17/1/94/62/1583877/user/1706318/htdocs/gallery/gallery1/511260974939a1b87a5dde3d99c0066f.jpg
Avatar of shareea

ASKER

Modified below line and got it to work. Thanks.

print "<img src=" . str_replace($_SERVER["DOCUMENT_ROOT"], "", $r["path"]) . " $attr border='0' />";