Hello, i have some code for adding images to a phpmyadmin database and then code for displaying the image, only problem is the code doesn't work!๐
If you can spot any errors please outline and suggst alternative ways.
My main problem is displaying a blob image in phpmyadmin on a website. If you have good code for this then that would be amazing!!
Uploading is not an issue at the moment as i can manually do it.
Many thanks, hoope to hear from you soon. Brian
Uploading image to database (index.php)
<?php
// database connection
$conn = mysql_connect("localhost", "web8_u1", "letmein")
OR DIE (mysql_error());
@mysql_select_db ("database", $conn) OR DIE (mysql_error());
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();
}
}
}
if ($GET) {
$iid = $GET["iid"];
$act = $_GET["act"];
switch ($act) {
case rem:
$sql = "DELETE FROM image WHERE image_id=$iid";
@ ($sql, $conn);
Header("Location:./index.php");
exit();
break;
default:
print "<img src="image.php?iid=$iid">";
break;
}
}
?>
<html>
<head>
<title>Storing Images in Database</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>
Showing Image in browser (showimg.php)
<?php
// database connection
$conn = mysql_connect("localhost", "web8_u1", "letmein")
OR DIE (mysql_error());
@mysql_select_db ("image", $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;
}
?>