Upload and fetch images from database (Mysql) in PHP
<html>
<body>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<input type="file" name="file" >
<input type="submit" name="submit" value="Upload">
</form>
</body>
</html>
<?php
$host = 'localhost';
$user = 'root';
$pw = '';
$db = 'Database name';
$con = mysql_connect($host,$user,$pw);
$result = mysql_query("SELECT * FROM `tablename`");
while ($row = mysql_fetch_array($result)) {
echo "<img src='".$row['image']."' height='150px' width='250px'></br>";
}
?>
<body>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<input type="file" name="file" >
<input type="submit" name="submit" value="Upload">
</form>
</body>
</html>
<?php
$host = 'localhost';
$user = 'root';
$pw = '';
$db = 'Database name';
$con = mysql_connect($host,$user,$pw);
mysql_select_db($db, $con);
if(@$_POST['submit'])
{
$query=null;
$file = $_FILES['file'];
$file_name = $file['name'];
$file_type = $file['type'];
$file_size = $file['size'];
$file_path = $file['tmp_name'];
if($file_name!="" && ($file_type="image/jpeg"|| $file_type="image/png"|| $file_type="image/gif") && $file_size<=1048576) {
if(move_uploaded_file($file_ path,'images/'.$file_name)){
$query = " INSERT INTO `tablename`(`id`, `name`, `image`) VALUES('','$file_name',' images/$file_name')";
$res = mysql_query("$query");
if($res == 'true')
{
echo "File uploaded!";
}
if(@$_POST['submit'])
{
$query=null;
$file = $_FILES['file'];
$file_name = $file['name'];
$file_type = $file['type'];
$file_size = $file['size'];
$file_path = $file['tmp_name'];
if($file_name!="" && ($file_type="image/jpeg"|| $file_type="image/png"|| $file_type="image/gif") && $file_size<=1048576) {
if(move_uploaded_file($file_
$query = " INSERT INTO `tablename`(`id`, `name`, `image`) VALUES('','$file_name','
$res = mysql_query("$query");
if($res == 'true')
{
echo "File uploaded!";
}
}
}
}
echo '</br>';
// Fetch data from database and show on front page. If you want to show in different page just copy the below code and paste on that page
echo '</br>';
// Fetch data from database and show on front page. If you want to show in different page just copy the below code and paste on that page
$result = mysql_query("SELECT * FROM `tablename`");
while ($row = mysql_fetch_array($result)) {
echo "<img src='".$row['image']."' height='150px' width='250px'></br>";
}
?>