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.
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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Here's sample of path in db:
/data/17/1/94/62/1583877/u ser/170631 8/htdocs/g allery/gal lery1/5112 60974939a1 b87a5dde3d 99c0066f.j pg
/data/17/1/94/62/1583877/u
ASKER
Modified below line and got it to work. Thanks.
print "<img src=" . str_replace($_SERVER["DOCU MENT_ROOT" ], "", $r["path"]) . " $attr border='0' />";
print "<img src=" . str_replace($_SERVER["DOCU
ASKER
Can you explain the image source portion of tag - particularly , "", ?
Open in new window