image is uploading in directory and in db also but when i refresh image disappear
database.php // i know i should not to use mysql but i have the it into mysqli so no big issue in this
<?php
//Connection
$con=mysql_connect("localhost","root","");
if(!$con)
echo "Failed to connect with MySQL.".mysql_error()."<br />";
//Create Database
$crt_db=mysql_query("CREATE DATABASE IF NOT EXISTS upload_images");
if(!$crt_db)
echo "Error in creating Database.".mysql_error()."<br />";
//Select Database
mysql_select_db("upload_images");
if(mysql_error())
echo "error in selecting database.".mysql_error()."<br />";
//Create Table
$crt_tbl=mysql_query("CREATE TABLE IF NOT EXISTS images(ID INT NOT NULL AUTO_INCREMENT,PRIMARY KEY(ID),image_name VARCHAR(255)) ");
if(mysql_error())
echo "Error in creating table ".mysql_error()."<br />";
?>
image_upload.php
<html>
<head>
<title>Image Upload</title>
<style>
body {font-family:Verdana, Geneva, sans-serif;}
</style>
</head>
<body>
<?php
//error handler function
function catchError($errno, $errmsg, $a, $b, $c)
{
echo "<b>Error:</b> [$errno] <br />$errmsg<br />";
echo "On Line=".$b;
}
//set error handler
set_error_handler("catchError");
require ('database.php');
?>
<div style="position:fixed; right:5px; top:5px">
<form method="post"><input type="submit" name="ds_btn" value="Destroy Session" /></form>
</div>
<?php
if(isset($_POST['submit']) && isset($_POST['hdn']))
{
if(isset($_POST['hdn']))
{
for($j=1;$j<=3;$j++)
{
if($_POST['hdn']==$j)
$code['img_name_'.$j]= $_FILES['file']['name'];
}
}
$img_name= $_FILES['file']['name'];
$img_type= $_FILES['file']['type'];
$img_size= $_FILES['file']['size'];
$img_tmp_path= $_FILES['file']['tmp_name'];
$img_error= $_FILES['file']['error'];
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $img_name);
$extension = end($temp);
if ( ( ($img_type == "image/gif")
|| ($img_type == "image/jpeg")
|| ($img_type == "image/jpg")
|| ($img_type == "image/pjpeg")
|| ($img_type == "image/x-png")
|| ($img_type == "image/png"))
&& ($img_size < 2000000)
&& in_array($extension, $allowedExts))
{
if ($img_error > 0)
{
echo "Return Code: " . $img_error . "<br>";
}
else
{
if (file_exists("upload/".$img_name))
{
echo $img_name . " already exists. <br /><br />";
}
else
{
move_uploaded_file($img_tmp_path,"upload/".$img_name);
echo "Stored in: " . "upload/".$img_name."<br /><br />";
$updt=mysql_query("INSERT INTO images (image_name) VALUES('".$_FILES['file']['name']."')");
if($updt)
echo 'Image name stored in database successfully.'."<br />";
else
echo 'Error in updating image name in database. Error:'.mysql_error()."<br />";
}
}
}
else
{
echo "Invalid file";
}
}
for($i=1;$i<=3;$i++)
{
?>
<div style="border:1px solid black; width:220px; height:260px;; padding:5px; margin:10px; float:left">
<form method="POST" enctype="multipart/form-data">
<table>
<tr>
<td><input type="file" name="file" id="file"><br></td>
<td><input type="hidden" name="hdn" value="<?php echo $i; ?>" /></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>
</form>
<?php
for($k=1;$k<=3;$k++)
{
if($i==$k && isset($code['img_name_'.$k]))
{
$sel=mysql_query("SELECT * FROM images WHERE image_name='".$code['img_name_'.$k]."'");
$row = mysql_fetch_array($sel);
$code['i_'.$k]=$row['image_name'];
echo '<img src="upload/'.$code['i_'.$k].'" height="180px" width="220px" />';
}
}
/*
if($i==2 && isset($_SESSION['img_name_2']))
{
$sel=mysql_query("SELECT * FROM images WHERE image_name='".$_SESSION['img_name_2']."'");
$view = mysql_fetch_array($sel);
$_SESSION['i_2']=$view['image_name'];
echo '<img src="upload/'.$_SESSION['i_2'].'" height="180px" width="220px" />';
}
if($i==3 && isset($_SESSION['img_name_3']))
{
$sel=mysql_query("SELECT * FROM images WHERE image_name='".$_SESSION['img_name_3']."'");
$view = mysql_fetch_array($sel);
$_SESSION['i_3']=$view['image_name'];
echo '<img src="upload/'.$_SESSION['i_3'].'" height="180px" width="220px" />';
}
*/
?>
</div>
<?php
}
?>
</body>
</html>
gampzz 0 Newbie Poster
mattster 195 Practically a Master Poster Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.