Storing Image using PHP

check_image.php

<?php
$link = mysql_connect(“localhost”,”root”,”rize@1251″)
or die(“Could not connect”.mysql_error());
mysql_select_db(“corephp”,$link) or die(mysql_error());

$sql = “CREATE TABLE IF NOT EXISTS images(
image_id INT(11) NOT NULL PRIMARY key AUTO_INCREMENT,
image_caption VARCHAR(255) NOT NULL,
image_username VARCHAR(255) NOT NULL,
image_date DATE not null)”;

$results = mysql_query($sql) or die(mysql_error());
echo “Table for storing images create successfully”;
?>

<html>
<head>
<title>Upload your picture</title>
</head>
<body>
<form name=”form1″ action=”check_image.php” enctype=”multipart/form-data” method=”post”>
Image Caption:<input type=”text” name=”image_caption” id=”image_caption”><br>
Username:<input type=”text” name=”image_username” id=”image_username”><br>
Upload Image:<input type=”file” name=”image_filename” id=”image_filename”><br>
<input type=”submit” name=”submit” value=”Upload”>
</form>
</body>
</html>

check_image.php

<?php
$link = mysql_connect(“localhost”,”root”,”rize@1251″) or die(“Could not connect now”);
mysql_select_db(“corephp”) or die(“Database not found”);
$image_caption = $_POST[‘image_caption’];
$image_username = $_POST[‘image_username’];
$image_tmpname = $_FILES[‘image_filename’][‘name’];
$today = date(“Y-m-d”);

$imgdir = “../test/uploads/”;
$imgname = $imgdir.$image_tmpname;
if(move_uploaded_file($_FILES[‘image_filename’][‘tmp_name’], $imgname))
{

list($width,$height,$type,$attr)= getimagesize($imgname);
switch($type)
{
case 1:
$ext = “.gif”; break;
case 2:
$ext = “.jpg”; break;
case 3:
$ext = “.png”; break;
default:
echo “Not acceptable format of image”;
}
$insert = “insert into images (image_caption, image_username, image_date)
values (‘$image_caption’,’$image_username’,’$today’)”;
$insertresults = mysql_query($insert) or die(mysql_error());

$last_pic_id = mysql_insert_id();
$newfilename = $imgdir.$last_pic_id.$ext;
rename($imgname,$newfilename);
}
?>
<img src=”uploads/<?php echo $last_pic_id.$ext; ?>” align=’center’>

 

http://blog.nerdstogeeks.com/2009/09/handling-images-using-php-mysql-in-3.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