<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="style.css">
<title>Image Upload in PHP and MYSQL database </title>
</head>
<body>
<?php
$dbHost = 'localhost';
$dbName = 'fileupnew';
$dbUsername = 'root';
$dbPassword = '';
$db= mysqli_connect($dbHost, $dbUsername, $dbPassword, $dbName);
?>
<?php
if(isset($_POST['form_submit']))
{
$n=$_POST['n'];
$fn=$_POST['fn'];
$mn=$_POST['mn'];
$folder = "basename/";
$image_file=$_FILES['image']['name'];
$file = $_FILES['image']['tmp_name'];
$path = $folder . $image_file;
$target_file=$folder.basename($image_file);
$imageFileType=pathinfo($target_file,PATHINFO_EXTENSION);
//Allow only JPG, JPEG, PNG & GIF etc formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
$error[] = 'Sorry, only JPG, JPEG, PNG & GIF files are allowed';
}
//Set image upload size
if ($_FILES["image"]["size"] > 1048576) {
$error[] = 'Sorry, your image is too large. Upload less than 1 MB in size.';
}
if(!isset($error))
{
//move image to the folder
move_uploaded_file($file,$target_file);
$result=mysqli_query($db,"INSERT INTO student(name,fathername,mobilenumber,image) VALUES('$n','$fn','$mn','$image_file')");
if($result)
{
$image_success=1;
}
else
{
echo 'Something went wrong';
}
}
}
if(isset($error)){
foreach ($error as $error) {
echo '<div class="message">'.$error.'</div><br>';
}
}
?>
<div class="container">
<?php if(isset($image_success))
{
echo '<div class="success">Image Uploaded successfully</div>';
} ?>
<div class="container">
<form action="" method="POST" enctype="multipart/form-data">
<center> <table style="background-color:#dcdcdc; border:5px red ridge;"><tr>
<th>Name</th><th><input type="text" name="n"></th></tr>
<tr><th>Father's Name</th><th><input type="text" name="fn"></th></tr>
<tr><th>Mobile Number</th><th><input type="text" name="mn"></th></tr>
<tr><th>Upload the student's image</th><th><input type="file" name="image"></th></tr>
<tr>
<br><br>
<button name="form_submit" class="btn-primary"> Upload</button>
</form>
</div>
<div class="container_display">
<table cellpadding="10">
<tr>
<th> name</th>
<th>father name</th>
<th>mobile number</th>
<th>image</th>
</tr>
<?php $res=mysqli_query($db,"SELECT* from student");
while($row=mysqli_fetch_array($res))
{
echo '<tr> <td>'.$row['name'].'</td>
<td>'.$row['fathername'].'</td>
<td>'.$row['mobilenumber'].'</td>
<td><img src="basename/'.$row['image'].'" height="200"></td>
</tr>';
} ?>
</table>
</div>
</body>
</html>
Steps:-
1. Make a folder to insert image files
2. name that folder in code in $folder
3. use folder/ in printing images
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="style.css">
<title>Image Upload in PHP and MYSQL database </title>
</head>
<body>
<?php
$dbHost = 'localhost';
$dbName = 'fileupnew';
$dbUsername = 'root';
$dbPassword = '';
$db= mysqli_connect($dbHost, $dbUsername, $dbPassword, $dbName);
?>
<?php
if(isset($_POST['form_submit']))
{
$n=$_POST['n'];
$folder = "pdf/";
$image_file=$_FILES['pdf_file']['name'];
$file = $_FILES['pdf_file']['tmp_name'];
$path = $folder . $image_file;
$target_file=$folder.basename($image_file);
$imageFileType=pathinfo($target_file,PATHINFO_EXTENSION);
//Allow only JPG, JPEG, PNG & GIF etc formats
//move image to the folder
move_uploaded_file($file,$target_file);
$result=mysqli_query($db,"INSERT INTO student2(title,pdf) VALUES('$n','$image_file')");
if($result)
{
$image_success=1;
}
else
{
echo 'Something went wrong';
}
}
?>
<div class="container">
<?php if(isset($image_success))
{
echo '<div class="success">Image Uploaded successfully</div>';
} ?>
<div class="container">
<form action="" method="POST" enctype="multipart/form-data">
<center> <table style="background-color:#dcdcdc; border:5px red ridge;"><tr>
<th>title</th><th><input type="text" name="n"></th></tr>
<tr><th>Upload the student's image</th><th><input type="file" name="pdf_file"></th></tr>
<tr>
<br><br>
<button name="form_submit" class="btn-primary"> Upload</button>
</form>
</div>
<div class="container_display">
<table cellpadding="10">
<tr>
<th>title</th>
<th>pdf</th>
</tr>
<?php $res=mysqli_query($db,"SELECT* from student2");
while($row=mysqli_fetch_array($res))
{
echo '
<td>'.$row['title'].'</td>
<td><a href="pdf/'.$row['pdf'].'" >f</td>
</tr>';
} ?>
</table>
</div>
</body>
</html>
Steps:-
1. Make a folder to insert image files
2. name that folder in code in $folder
3. use folder/ in printing images