I need to upload an image to database and path should be saved in table. I am using the below code to upload the image and store the path in database table. I have created a directory "images" for storing image which is uploaded by user.
if(isset($_POST['submit']))
{
$errors= array();
$file_name=$_FILES['photo']['name'];
$file_size =$_FILES['photo']['size'];
$file_tmp =$_FILES['photo']['tmp_name'];
$file_type=$_FILES['photo']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['photo']['name'])));
$expensions= array("jpeg","jpg","png");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"images/".$file_name);
echo '<script language="javascript">';
echo 'alert("Success")';
echo '</script>';
}
else
{
echo '<script language="javascript">';
echo 'alert("Failed")';
echo '</script>';
print_r($errors);
}
}
$query = mysql_query("insert into e(photo) values ('$file_name')");
echo '<script language="javascript">';
echo 'alert("Ok")';
echo '</script>';
}
Then the image should be displayed from database using the stored path in table. I am using the below code to display the image from database.
$connection = mysql_connect("localhost", "root", "");
$db = mysql_select_db("s", $connection);
$sql = "SELECT * FROM e";
$result = mysql_query($sql);
while ($o = mysql_fetch_assoc($result))
{
echo "<tbody>";
echo "<tr>";
echo "<td>".'<img src="$photo;"' ."</td>";
echo "</tr>";
echo "</tbody>";
}
?>