Upload image in PHP & Store Image Name,Path into MySQL database

How to upload media file online on server using PHP script and send image name with extension inside MySQL db.

Dynamic image uploading is one of the most popular feature for all large php websites and mostly used into social networking platform where user can upload their photos online on server. So in this tutorial we are creating a PHP script that’s allow us to upload media files online on server inside our already created folder and in between uploading process it will save the media file name with its extension into MySQL database rows . So it can be easily retrievable again. So here is the complete step by step tutorial for Upload image in PHP & Store Image Name,Path into MySQL database.

android-project-download-code-button

How to Upload image in PHP & Store Image Name,Path into MySQL database.

Code for index.php file.

 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Upload image in PHP & Store Image Name,Path into MySQL database</title>
<link href="style/login-style.css" rel="stylesheet" type="text/css">
</head>
<body>

<form action="" method="post" enctype="multipart/form-data">
<table border="2" cellpadding="15" cellspacing="2" width="430" align="center">
<tr class="tabheader">
<td align="center" colspan="2">Image Upload & Save into MySQL db</td>
</tr>
<tr class="row">
<td>Select File Here:</td>
<td><input type="file" name="uploadImage" id="uploadImage"></td>
</tr>
<tr class="tabheader">
<td colspan=2><p align=center>
<input type="submit" value="Click Here To Upload" name="submit">
</td>
</tr>
</table>
</form>

<?php

//Adding isset button function on submit button.
if(isset($_POST["submit"])) {
 
 if (!empty($_FILES["uploadImage"]["name"])) {

include 'dbconfig.php';

$ImageSavefolder = "images/";

move_uploaded_file($_FILES["uploadImage"]["tmp_name"] , "$ImageSavefolder".$_FILES["uploadImage"]["name"]);

mysql_query("INSERT into image_upload (image_name) VALUES('".$_FILES['uploadImage']['name']."')");

if($conn) { 

echo '<p align="center"> Image name successfully saved into MySQL db.</p>'; 

}

else {
 
echo '<p align="center"> Sorry, Please try again.</p>';
}
 }
 else {
 
 echo '<p align="center"> Select file to upload </p>';
 
 }

 }
 
?>


</body>
</html>

Code for dbconfig.php file.

 <?php

//Define your host here.
$hostname = "localhost";

//Define your database username here.
$username = "root";

//Define your database password here.
$password = "";

//Define your database name here.
$dbname = "test";

 $conn = mysql_connect($hostname, $username, $password);
 
 if (!$conn)
 
 {
 
 die('Could not connect: ' . mysql_error());
 
 }
 
 mysql_select_db($dbname, $conn);



?>

Code for login-style.css and this file is created into style named folder .

 @charset "utf-8";
/* CSS Document */

.tabheader {
background-color:#EF5350;
color:white;
font-weight:bold;
}
.row {
background-color: #EF9A9A;
color:white;
}

Screenshots:

Upload image in PHP & Store Image Name,Path into MySQL database

upload-complete

Click here to download Upload image in PHP & Store Image Name,Path into MySQL database project with all the source code + MySQL db file included.