How to upload a image in MySQL database in php

here I just tested the code.. at first you need to create a database and create a table with two field named by “fileloc” and “id” and also create a folder “pic” in apache htdocs.

then save the following code as    upload.html………..

<form action=”upload.php” method=”post”
enctype=”multipart/form-data”>
<label for=”file”>Upload a picture:</label>
<p>
<input type=”file” name=”file”  />
</p>
<p><br />
Enter id
<input name=”id” type=”text”>
</p>
<p>
<input type=”submit” name=”submit” value=”Submit” />
</p>
</form>

after saving this open a new window and write the following code and save as upload.php

<?php
if ((($_FILES[“file”][“type”] == “image/gif”)
|| ($_FILES[“file”][“type”] == “image/jpeg”)
|| ($_FILES[“file”][“type”] == “image/pjpeg”))
&& ($_FILES[“file”][“size”] < 400000))
{
if ($_FILES[“file”][“error”] > 0)
{
echo “Return Code: ” . $_FILES[“file”][“error”] . “<br />”;
}
else
{
echo “Upload: ” . $_FILES[“file”][“name”] . “<br />”;

if (file_exists(“pic/” . $_FILES[“file”][“name”]))
{
echo $_FILES[“file”][“name”] . ” already exists. “;
}
else
{
move_uploaded_file($_FILES[“file”][“tmp_name”],”pic/” . $_FILES[“file”][“name”]);
echo “Stored in: ” . “pic/” . $_FILES[“file”][“name”];
}
}
}
else
{
echo “Invalid file”;
}

?>

<?php include(“dbconnect.php”);?>

<? $id=$_POST[‘id’];
$fileloc=”pic”.”/”.$_FILES[“file”][“name”];
echo $id;
echo $fileloc;
$query=”INSERT INTO fileupload(id,fileloc) VALUES (‘$id’,’$fileloc’)”;
$d=@mysql_query($query);
if ($d)
echo “Success”;

?>

yaa done ! now try yourself..

Advertisements
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: