Programming in almost language

This is the site where you may share your knowledge and experience to eachother..

  • Categories

  • LinkedIn

  • Tweet Me

  • My footsteps

Code for Uploading Image in Database

Posted by Praveen Kumar on November 4, 2008

(Posted by Rishi Chandwani)

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;

<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />

<title>Image Upload</title>
</head>
<body>

<?php
if(!isset($_FILES[‘TxtImg’]))
{
echo ‘<p>Please select a file</p>’;
}
else
{
try
{
upload();
echo ‘<p>Thank you for submitting</p>’;
}
catch(Exception $e)
{
echo $e->getMessage();
echo ‘Sorry, could not upload file’;
}
}

function upload()
{
$maxsize=20000000;
if(is_uploaded_file($_FILES[‘TxtImg’][‘tmp_name’]))
{
// check the file is less than the maximum file size
if($_FILES[‘TxtImg’][‘size’] < $maxsize)
{
// prepare the image for insertion
$imgData=addslashes(file_get_contents($_FILES[‘TxtImg’][‘tmp_name’]));
// $imgData = addslashes($_FILES[‘userfile’]);

// put the image in the db…
// database connection

mysql_connect(“localhost”, “$username”, “$password”) or die(“Unable to Connect”);

// select the db
mysql_select_db (“$db”) or die(“Unable to select db”);


// our sql query
$sql=”insert into test values (‘$fnm’, ‘$lnm’, ‘$imgData’)”;

// insert the image
if(!mysql_query($sql))
{
echo ‘Unable to upload file’;
}
else
{

echo ‘Upload Successful’;
}
}
}
else
{
echo “File Size Greater than Required”;
}
}
?>

</body>
</html>

Advertisements

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: