• support@answerspoint.com

how to upload file in mysql database using php

2206

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.

2Answer


0

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

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

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

 

file_upload.php

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

<?php 
	include("config.php");
	session_start();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>file Upload</title>
</head>

<body>
<?php
if($_POST)
{
//var_dump($_FILES);
//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

$description=$_POST['description'];
$pic=($_FILES['image']['name']);

//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"> 
    <p>Description</p>
    <textarea rows="10" cols="35" name="description"></textarea>
    <br/>
    <input TYPE="submit" name="upload" value="Add"/>
</form>


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

</body>
</html>

 

  • answered 6 years ago
  • Community  wiki

0

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 2 years ago
  • Abha Sharma

Your Answer

    Facebook Share        
       
  • asked 6 years ago
  • viewed 2206 times
  • active 6 years ago

Best Rated Questions