hello,
i am trying to store images in a database and then show them depending on what user is logged in . i wrote ( and borrowed) the following code :
<?
ob_start();
include "include/session.php";
?>
<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title>HALL OF FAME</title>
<meta name="GENERATOR" content="Arachnophilia 4.0">
<meta name="FORMATTER" content="Arachnophilia 4.0">
</head>
<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">
</body>
</html>
<?
$host = "localhost";
$user = "********";
$pass = "********";
$db = "******_hallfame";
$table= "image";
$conn = mysql_connect($host, $user, $pass)
OR DIE (mysql_error());
@mysql_select_db ($db, $conn) OR DIE (mysql_error());
echo "<center><font face='Verdana' size='2' >Welcome $session[userid]<br><br> <br><br><br>Click <a href=logout.php>here to logout</a><br></center></font>";
// 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",
"application/octet-stream");
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 ".$table." "
. "(image_type, image, image_size, image_name, image_date, person_ID) ";
$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 'view':
$sql = "select * from ".$table." where image_id=" .$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;
}
break;
case 'rem':
$sql = "DELETE FROM ".$table." WHERE image_id=" .$iid;
@mysql_query ($sql, $conn);
Header("Location:".$_SERVER["PHP_SELF"]);
exit();
break;
default:
print "<img src=\"image.php?iid=$iid\">";
break;
}
}
#
#FUNCTIONS
#
function get_filesize ($dsize)
{
if (strlen($dsize) <= 9 && strlen($dsize) >= 7)
{
$dsize = number_format($dsize / 1048576,1);
return "$dsize MB";
}
elseif (strlen($dsize) >= 10)
{
$dsize = number_format($dsize / 1073741824,1);
return "$dsize GB";
}
else
{
$dsize = number_format($dsize / 1024,1);
return "$dsize KB";
}
}
?>
<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
//echo "$session[userid]<br>";
$tabletwo = "plus_signup";
$sqll = "select plus_signup.person_ID from plus_signup where plus_signup.userid = '".$session['userid'] . "'";
//echo " sqll = $sqll<br>";
$user = mysql_query($sqll);
//echo "username = $user<br>";
list ($UserName) = mysql_fetch_array ($user);
//echo" $UserName<br>";
$sql = "SELECT * FROM ".$table." where person_ID = ".$UserName." ORDER BY image_id";
$result = mysql_query ($sql, $conn);
//echo " sql = $sql<br>";
$i=0;
$str='';
if (mysql_num_rows($result)>0)
{
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$i++;
$str .= $i.". ";
$str .= "<a href=\"welcome.php?act=view&iid=".$row["image_id"]."\">";
$str .= "<img border=\"0\" height=\"90\" width=\"100\" src=\"image.php?act=view&iid=".$row["image_id"]."\"></a> ";
$str .= "[Name: ".$row["image_name"]."] ";
$str .= "[Date: ".$row["image_date"]."] ";
$str .= "[Size: ". get_filesize($row["image_size"])."] ";
$str .= "[<a href=\"welcome.php?act=rem&iid=".$row["image_id"]."\">Remove</a>]<br>";
}
print $str;
}
ob_end_flush();
?>
</body>
</html>
</body>
</html>
somehow the images dont get added to the database.. anyone?