• support@answerspoint.com

how to upload file in mysql database using php


I Want to upload a image file im my sql database through php. we create a database and connect to my image upload page and create a form on this page and send my value in my database through mysql Query. but this is not sucesses. 

I am learner, so please explain it in simpal way.



If you want to upload any file in Mysql database through php then at first you have to create database. if you are using mysql use phpmyadmin to create database.


Config.php file is having information about MySQL Data base configuration.

	$dbhost = 'localhost';
	$dbuser = 'root';
	$dbpass = '';
	$dbname = 'image_upload';
	$con = mysql_connect($dbhost, $dbuser, $dbpass);
	if(! $con ) 
		die('Could not connect: ' . mysql_error());
	//echo 'Connected successfully';



file_upload.php is having information about php script and HTML to upload file.

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>file Upload</title>

//This is the directory where images will be saved
$target = "image/";
$target = $target . basename( $_FILES['image']['name']);

//This gets all the other information from the form


//Writes the information to the database
$query=" insert into image_upload set image_detail= '$description', imge_name='$pic' ";
$ex_query= mysql_query($query) or die(mysql_error());

//Writes the Filename to the server
if(move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
    //Tells you if its all ok
    echo "The file  has been uploaded, and your information has been added to the directory";
} else {
    //Gives and error if its not
    echo "Sorry, there was a problem uploading your file.";

<form method="post" enctype="multipart/form-data">
    <input type="file" name="image"> 
    <textarea rows="10" cols="35" name="description"></textarea>
    <input TYPE="submit" name="upload" value="Add"/>

$query2=" select * from image_upload where status='on'";
$select_query=mysql_query($query2) or die(mysql_error());
while($row = mysql_fetch_array($select_query))
		<td><?php echo $row['id']; ?></td>
		<td> <img src="image/<?php echo $row['imge_name'];?>"   /> </td>
        <td> <?php echo $row['image_detail']; ?></td>



  • answered 5 years ago
  • Community  wiki


Get the file extension using pathinfo() function in PHP and validate the file format to check whether the user selects an image file. Upload image to server using move_uploaded_file() function in PHP.

  • answered 1 year ago
  • Abha Sharma

Your Answer

    Facebook Share        
  • asked 5 years ago
  • viewed 1929 times
  • active 5 years ago

Best Rated Questions