<?php
// index.php - by Hermawan Haryanto <hermawan@codewalkers.com>
// Example PHP Script, demonstrating Storing Image in Database
// Detailed Information can be found at http://www.codewalkers.com
// database connection
$conn = mysql_connect("localhost", "username", "password")
OR DIE (mysql_error());
@mysql_select_db ("database", $conn) OR DIE (mysql_error());
// Do this process if user has browse the
// file and click the submit button
if ($_FILES) {
$image_types = Array ("image/bmp",
"image/jpeg",
"image/pjpeg",
"image/gif",
"image/x-png");
if (is_uploaded_file ($_FILES[“userfile”][“tmp_name”])) {
$userfile = addslashes (fread
(fopen ($_FILES["userfile"]["tmp_name"], "r"),
filesize ($_FILES["userfile"]["tmp_name"])));
$file_name = $_FILES["userfile"]["name"];
$file_size = $_FILES["userfile"]["size"];
$file_type = $_FILES["userfile"]["type"];
if (in_array (strtolower ($file_type), $image_types)) {
$sql = "INSERT INTO image "
. "(image_type, image, image_size, image_name, image_date) ";
$sql.= "VALUES (";
$sql.= "'{$file_type}', '{$userfile}', '{$file_size}', "
. "'{$file_name}', NOW())";
@mysql_query ($sql, $conn);
Header("Location:".$_SERVER["PHP_SELF"]);
exit();
}
}
}
// Do this process of user has click
// a file name to view or remove
if ($_GET) {
$iid = $_GET["iid"];
$act = $_GET["act"];
switch ($act) {
case rem:
$sql = "DELETE FROM image WHERE image_id=$iid";
@mysql_query ($sql, $conn);
Header("Location:./index.php");
exit();
break;
default:
print "<img src="image.php?iid=$iid">";
break;
}
}
?>
<html>
<head>
<title>Storing Images in DB</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
Select Image File:
<input type="file" name="userfile" size="40">
<input type="submit" value="submit">
</form>
<?php
$sql = "SELECT * FROM image ORDER BY image_date DESC";
$result = mysql_query ($sql, $conn);
if (mysql_num_rows($result)>0) {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$i++;
$str .= $i.". ";
$str .= "<a href='index.php?iid=".$row["image_id"]."'>"
. $row["image_name"]."</a> ";
$str .= "[".$row["image_date"]."] ";
$str .= "[".$row["image_size"]."] ";
$str .= "[<a href='index.php?act=rem&iid=".$row["image_id"]
. "'>Remove</a>]<br>";
}
print $str;
}
?>
</body>
</html>
<?php
// image.php - by Hermawan Haryanto <hermawan@dmonster.com>
// Example PHP Script, demonstrating Storing Image in Database
// Detailed Information can be found at http://www.codewalkers.com
// database connection
$conn = mysql_connect("localhost", "user", "password")
OR DIE (mysql_error());
@mysql_select_db ("hermawan", $conn) OR DIE (mysql_error());
$sql = "SELECT * FROM image WHERE image_id=".$_GET["iid"];
$result = mysql_query ($sql, $conn);
if (mysql_num_rows ($result)>0) {
$row = @mysql_fetch_array ($result);
$image_type = $row["image_type"];
$image = $row["image"];
Header ("Content-type: $image_type");
print $image;
}
?>
15 Replies - 5413 Views - Last Post: 14 January 2011 - 03:53 AM
#1
Not able fetch the image from mysql database and to display it.
Posted 10 January 2011 - 05:21 AM
Replies To: Not able fetch the image from mysql database and to display it.
#2
Re: Not able fetch the image from mysql database and to display it.
Posted 10 January 2011 - 05:37 AM
#3
Re: Not able fetch the image from mysql database and to display it.
Posted 10 January 2011 - 05:59 AM
#4
Re: Not able fetch the image from mysql database and to display it.
Posted 10 January 2011 - 06:16 AM
#5
Re: Not able fetch the image from mysql database and to display it.
Posted 10 January 2011 - 09:39 PM
#6
Re: Not able fetch the image from mysql database and to display it.
Posted 11 January 2011 - 12:25 AM
$sql = "SELECT * FROM image ORDER BY image_date DESC"; $result = mysql_query ($sql, $conn) or die(mysql_error());
and see what happens, if it executes successfully, then check the number of rows returned-
echo mysql_num_rows($result);
This post has been edited by atik97: 11 January 2011 - 12:42 AM
#7
Re: Not able fetch the image from mysql database and to display it.
Posted 12 January 2011 - 10:03 PM
$sql = "SELECT * FROM image WHERE image_id=".$_GET["iid"]; $result = mysql_query ($sql, $conn); print mysql_num_rows ($result);But still the problem is it is not displaying the fetched image. I mean that the below code is not displaying the image.
if (mysql_num_rows ($result)>0) {
$row = @mysql_fetch_array ($result);
$image_type = $row["image_type"];
$image = $row["image"];
Header ("Content-type: $image_type");
print $image;
}
#8
Re: Not able fetch the image from mysql database and to display it.
Posted 13 January 2011 - 03:15 AM
PS. not sure if the > is intended … if it is, change it to >
This post has been edited by Dormilich: 13 January 2011 - 03:17 AM
#9
Re: Not able fetch the image from mysql database and to display it.
Posted 13 January 2011 - 06:12 AM
// database connection
$conn = mysql_connect("localhost", "root", "")
OR DIE (mysql_error());
mysql_select_db ("justgetit", $conn) OR DIE (mysql_error());
$sql = "SELECT * FROM image WHERE image_id=".$_GET["iid"];
$result = mysql_query ($sql, $conn);
print mysql_num_rows ($result);
if (mysql_num_rows ($result)>0) {
$row = mysql_fetch_object ($result);
$image_type = $row->image_type;
$image = $row->image;
Header ("Content-type: $image_type");
print $image;
}
But still I could not get the output.
#10
Re: Not able fetch the image from mysql database and to display it.
Posted 13 January 2011 - 07:41 AM
if (is_uploaded_file ($_FILES[“userfile”][“tmp_name”]))
This is just a sample of code with invalid character, There are same case in lots of areas in your code, html entities are being in use in the code you provided instead of special characters like "<", ">", "&". Correct those portions. I don't know if you have put all the code in same file or created two separate files. Actually bottom part will be place in separate file named image.php.
I will ask you to made another change in your index.php file.
$str .= "<a href='index.php?iid=".$row["image_id"]."'>" 077 . $row["image_name"]."</a> ";
Replace index.php with image.php, as image.php file is responsible for displaying image, not the index.php file. index.php file will only show link to display image or delete the image. Made all the corrections and let me know if it works or not. If you can made all the corrections i have mentioned, there is no reason to fail the script.
#11
Re: Not able fetch the image from mysql database and to display it.
Posted 14 January 2011 - 01:09 AM
<?php // database connection
$conn = mysql_connect("localhost", "root", "")
OR DIE (mysql_error());
mysql_select_db ("justgetit", $conn) OR DIE (mysql_error());
// Do this process if user has browse the
// file and click the submit button
if ($_FILES) {
$image_types = Array ("image/bmp",
"image/jpeg",
"image/pjpeg",
"image/gif",
"image/x-png");
if (is_uploaded_file ($_FILES["userfile"]["tmp_name"])) {
$userfile = addslashes (fread
(fopen ($_FILES["userfile"]["tmp_name"], "r"),
filesize ($_FILES["userfile"]["tmp_name"])));
$file_name = $_FILES["userfile"]["name"];
$file_size = $_FILES["userfile"]["size"];
$file_type = $_FILES["userfile"]["type"];
if (in_array (strtolower ($file_type), $image_types)) {
$sql = "INSERT INTO image "
. "(image_type, image, image_size, image_name, image_date) ";
$sql.= "VALUES (";
$sql.= "'{$file_type}', '{$userfile}', '{$file_size}', "
. "'{$file_name}', NOW())";
$res=mysql_query ($sql, $conn);
Header("Location:".$_SERVER["PHP_SELF"]);
exit();
}
}
}
// Do this process of user has click
// a file name to view or remove
if ($_GET) {
$iid = $_GET["iid"];
$act = $_GET["act"];
switch ($act) {
case rem:
$sql = "DELETE FROM image WHERE image_id=$iid";
$res1=mysql_query ($sql, $conn);
Header("Location: index.php");
exit();
break;
default:
Header("Location: image.php?iid=$iid");
break;
}
}
?>
<html>
<head>
<title>Storing Images in DB</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
Select Image File:
<input type="file" name="userfile" size="40">
<input type="submit" value="submit">
</form>
<?php
$sql = "SELECT * FROM image ORDER BY image_date DESC";
$result = mysql_query ($sql, $conn);
if (mysql_num_rows($result)>0) {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$i++;
$str .= $i.". ";
$str .= "<a href='image.php?iid=".$row["image_id"]."'>"
. $row["image_name"]."</a> ";
$str .= "[".$row["image_date"]."] ";
$str .= "[".$row["image_size"]."] ";
$str .= "[<a href='index.php?act=rem&iid=".$row["image_id"]
. "'>Remove</a>]<br>";
}
print $str;
}
?>
</body>
</html>
And the image.php is as follows:
<?php
// image.php
// database connection
$conn = mysql_connect("localhost", "root", "")
OR DIE (mysql_error());
mysql_select_db ("justgetit", $conn) OR DIE (mysql_error());
$sql = "SELECT * FROM image WHERE image_id=".$_GET["iid"];
$result = mysql_query ($sql, $conn);
print mysql_num_rows ($result);
if (mysql_num_rows ($result)>0)
{
$row = mysql_fetch_object ($result);
$image_type = $row->image_type;
$image = $row->image;
Header ("Content-type: $image_type");
print $image;
}
?>
Also I tried the last change that you have asked me:"Replace index.php with image.php, as image.php file is responsible for displaying image, not the index.php file."i.e.,I used the below code but still the image is not getting displayed.
$str .= "<a href='image.php?iid=".$row["image_id"]."'>"
. $row["image_name"]."</a> ";
#12
Re: Not able fetch the image from mysql database and to display it.
Posted 14 January 2011 - 01:16 AM
index.php file
<?php
// index.php - by Hermawan Haryanto <hermawan@codewalkers.com>
// Example PHP Script, demonstrating Storing Image in Database
// Detailed Information can be found at http://www.codewalkers.com
// database connection
$conn = mysql_connect("localhost", "user", "password")
OR DIE (mysql_error());
@mysql_select_db ("dbname", $conn) OR DIE (mysql_error());
// Do this process if user has browse the
// file and click the submit button
if ($_FILES) {
$image_types = Array ("image/bmp",
"image/jpeg",
"image/pjpeg",
"image/gif",
"image/x-png");
if (is_uploaded_file ($_FILES["userfile"]["tmp_name"])) {
$userfile = addslashes (fread
(fopen ($_FILES["userfile"]["tmp_name"], "r"),
filesize ($_FILES["userfile"]["tmp_name"])));
$file_name = $_FILES["userfile"]["name"];
$file_size = $_FILES["userfile"]["size"];
$file_type = $_FILES["userfile"]["type"];
if (in_array (strtolower ($file_type), $image_types)) {
$sql = "INSERT INTO image "
. "(image_type, image, image_size, image_name, image_date) ";
$sql.= "VALUES (";
$sql.= "'{$file_type}', '{$userfile}', '{$file_size}', "
. "'{$file_name}', NOW())";
@mysql_query ($sql, $conn);
Header("Location:".$_SERVER["PHP_SELF"]);
exit();
}
}
}
// Do this process of user has click
// a file name to view or remove
if ($_GET) {
$iid = $_GET["iid"];
$act = $_GET["act"];
switch ($act) {
case 'rem':
$sql = "DELETE FROM image WHERE image_id=$iid";
@mysql_query ($sql, $conn);
Header("Location:./index.php");
exit();
break;
default:
print "<img src=\"image.php?iid=$iid\">";
break;
}
}
?>
<html>
<head>
<title>Storing Images in DB</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
Select Image File:
<input type="file" name="userfile" size="40">
<input type="submit" value="submit">
</form>
<?php
$sql = "SELECT * FROM image ORDER BY image_date DESC";
$result = mysql_query ($sql, $conn);
if (mysql_num_rows($result)>0) {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$i++;
$str .= $i.". ";
$str .= "<a href='image.php?iid=".$row["image_id"]."'>"
. $row["image_name"]."</a> ";
$str .= "[".$row["image_date"]."] ";
$str .= "[".$row["image_size"]."] ";
$str .= "[<a href='index.php?act=rem&iid=".$row["image_id"]
. "'>Remove</a>]<br>";
}
print $str;
}
?>
</body>
</html>
image.php file
<?php
// image.php - by Hermawan Haryanto <hermawan@dmonster.com>
// Example PHP Script, demonstrating Storing Image in Database
// Detailed Information can be found at http://www.codewalkers.com
// database connection
$conn = mysql_connect("localhost", "user", "password")
OR DIE (mysql_error());
@mysql_select_db ("dbname", $conn) OR DIE (mysql_error());
$sql = "SELECT * FROM image WHERE image_id=".$_GET["iid"];
$result = mysql_query ($sql, $conn);
if (mysql_num_rows ($result)>0) {
$row = @mysql_fetch_array ($result);
$image_type = $row["image_type"];
$image = $row["image"];
Header ("Content-type: $image_type");
print $image;
}
?>
It should work, just change the database settings to match your own. Let me know if there is any further error occurs or not.
#13
Re: Not able fetch the image from mysql database and to display it.
Posted 14 January 2011 - 03:43 AM
print mysql_num_rows ($result);Keep your good work on. Meet you with another post. Khuda Hafees(Bye).
#14
Re: Not able fetch the image from mysql database and to display it.
Posted 14 January 2011 - 03:48 AM
Happy to know its finally working & you are always welcome.
#15
Re: Not able fetch the image from mysql database and to display it.
Posted 14 January 2011 - 03:53 AM
|
|

New Topic/Question
Reply




MultiQuote






|