Upload image in php

Image Insert, Update, Delete with allow image extension and save image with rename using time();

Image saved in upload folder and when you delete record, image is also delete from upload directory.

form.php

<form method=”post” enctype=”multipart/form-data”>
id
<input type=”text” name=”id”>
name
<input type=”text” name=”name”>
image
<input type=”file” name=”image”>
<br>
<input type=”submit” name=”submit” value=”submit”>
</form>
<?php

$cn=mysql_connect(“localhost”,”root”,””);
$db=mysql_select_db(“image”,$cn);

if(isset($_POST[‘submit’]))
{
$id = $_POST[‘id’];
$name = $_POST[‘name’];

$define_name = “profile_”.time();

$image = $define_name.”.jpg”;

$allowedExts = array(“gif”, “jpeg”, “jpg”, “png”);
if (($_FILES[“image”][“type”] == “image/gif”)
|| ($_FILES[“image”][“type”] == “image/jpeg”)
|| ($_FILES[“image”][“type”] == “image/png”)
|| ($_FILES[“image”][“type”] == “image/pjpeg”))

{
move_uploaded_file($_FILES[‘image’][‘tmp_name’],”upload/”.$define_name.”.jpg”);
echo “inserted”;
$q=’INSERT INTO `img`(`id`,`name`,`image`) VALUES (“‘.$id.'”,”‘.$name.'”,”‘.$image.'”)’;
$r=mysql_query($q);
}
else
{
echo ‘alert(“wrong file extension!”)’;
}
}
include “display.php”;
?>

display.php

<?php
$cn=mysql_connect(“localhost”,”root”,””);
$db=mysql_select_db(“image”,$cn);
$q=’SELECT * FROM `img`’;
$r=mysql_query($q);
?>
<table border=”1″>
<tr>
<td>id</td>
<td>name</td>
<td>image</td>
</tr>
<?php
while($row= mysql_fetch_array($r))
{ ?>
<tr>
<td><?php echo $row[‘id’]; ?></td>
<td><?php echo $row[‘name’]; ?></td>
<td><img src=”upload/<?php echo $row[‘image’]; ?>” width=50 height=50></td>
<td><a href=”edit.php?editID=<?php echo $row[‘id’]; ?>”>edit</a></td>
<td><a href=”delete.php?deleteID=<?php echo $row[‘id’]; ?>&image=<?php echo $row[‘image’]; ?>”>delete</a></td>
</tr>
<?php
}
?>
</table>

editt.php

<?php
$cn=mysql_connect(“localhost”,”root”,””);
$db=mysql_select_db(“image”,$cn);
if(isset($_REQUEST[‘editID’]))
{
$id = $_REQUEST[‘editID’];
$q = mysql_query(“SELECT * FROM img WHERE id = ‘$id'”)or die(mysql_error());
$r = mysql_fetch_array($q);
}

?>

<form method=”post” enctype=”multipart/form-data”>
id
<input type=”text” name=”id” value=”<?php echo $r[‘id’]; ?>”>
name
<input type=”text” name=”name” value=”<?php echo $r[‘name’]; ?>”>
image
<input type=”file” name=”image” value=”<?php echo $r[‘image’]; ?>”>
<img src=”upload/<?php echo $r[‘image’]; ?>” width=50 height=50> <br>
<a href=”form.php”>display</a>
<input type=”submit” name=”update” value=”update”>
<input type=”hidden” value=”<?php echo $r[‘image’]; ?>” name=”himage” />
</form>
<?php
if(isset($_POST[“update”]))
{
$id=$_POST[‘id’];
$name=$_POST[‘name’];
$image=$_FILES[‘image’][‘name’];
$oldimage = $_REQUEST[‘himage’];
unlink(‘upload/’.$oldimage);
move_uploaded_file($_FILES[‘image’][‘tmp_name’],”upload/”.$_FILES[‘image’][‘name’]);
$q=’UPDATE `img` SET `name`=”‘.$name.'”, `image`=”‘.$image.'” WHERE `id`=”‘.$id.'” ‘;
$r=mysql_query($q) or die (mysql_error());
echo “updated”;
header(“location:form.php”);
}
?>

delete.php

<?php

$cn=mysql_connect(“localhost”,”root”,””);
$db=mysql_select_db(“image”,$cn);

if(isset($_REQUEST[‘deleteID’]))
{
$deleteID=$_REQUEST[‘deleteID’];
$image=$_REQUEST[‘image’];

$q=’DELETE FROM `img` WHERE `id` = “‘.$deleteID.'”‘;
$r=mysql_query($q) or die (mysql_error());

unlink(“upload/”.$image);

echo “deleted”;
header(‘location:form.php’);
}
?>

database.sql

— Database: `image`

— ——————————————————–


— Table structure for table `img`

CREATE TABLE IF NOT EXISTS `img` (
`id` int(50) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`image` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=110 ;


— Dumping data for table `img`

INSERT INTO `img` (`id`, `name`, `image`) VALUES
(24, ‘sss’, ‘1.jpg’);

Advertisements

Tagged: ,

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: