Programming in almost language

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

Archive for the ‘PHP’ Category

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”>
<html xmlns=”http://www.w3.org/1999/xhtml”>

<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>

Posted in PHP | Leave a Comment »