0

Hi..Can anyone help me out? Can anyone give me a sample image uploader in php? That it will store to php mysql database? With 4 fields namely(id, image_name, images, descriptions).

These are my fields.

<form action="" method="post">
<input name="image_name" type="text" id="image_name" value=""/>
<input type="file" name="images" id="images">
<input name="descriptions" type="text" id="descriptions" value=""/>
<input type="submit" name="Submit" value="Submit"/>
</form>

Edited by rolyestemonio: n/a

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by rolyestemonio
0
<?php
$con=mysql_connect("localhost", "username", "password")or die("cannot connect");
mysql_select_db("db_name",$con)or die("cannot select DB");
if(isset($_POST['Submit']))
{
	$img_name = $_FILES['images']['name'];
	$image_name = $_POST['image_name'];
	$descriptions = $_POST['descriptions'];
	$img_tmp_name = $_FILES['images']['tmp_name'];
	copy($img_tmp_name,"upload/".$img_name);
	$qry = "INSERT INTO `tab_name`(`id`, `image_name`, `descriptions`) VALUES(NULL,'$image_name','$descriptions')";
	$res = mysql_query($qry);
	if($res) echo "Data Saved";
	else echo "Sorry, data not saved, try again";
}
?>
<!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=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="" method="post" enctype="multipart/form-data">
<input name="image_name" type="text" id="image_name" value=""/><br />
<input type="file" name="images" id="images"><br />
<input name="descriptions" type="text" id="descriptions" value=""/><br />
<input type="submit" name="Submit" value="Submit"/>
</form>
<img src="<?php echo "upload/".$_FILES['images']['name']; ?>" width="100" height="100" />
</body>
</html>

I think you don't need 'image_name' field.

0
<?php
$con=mysql_connect("localhost", "username", "password")or die("cannot connect");
mysql_select_db("db_name",$con)or die("cannot select DB");
if(isset($_POST['Submit']))
{
	$img_name = $_FILES['images']['name'];
	$image_name = $_POST['image_name'];
	$descriptions = $_POST['descriptions'];
	$img_tmp_name = $_FILES['images']['tmp_name'];
	copy($img_tmp_name,"upload/".$img_name);
	$qry = "INSERT INTO `tab_name`(`id`, `image_name`, `descriptions`) VALUES(NULL,'$image_name','$descriptions')";
	$res = mysql_query($qry);
	if($res) echo "Data Saved";
	else echo "Sorry, data not saved, try again";
}
?>
<!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=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="" method="post" enctype="multipart/form-data">
<input name="image_name" type="text" id="image_name" value=""/><br />
<input type="file" name="images" id="images"><br />
<input name="descriptions" type="text" id="descriptions" value=""/><br />
<input type="submit" name="Submit" value="Submit"/>
</form>
<img src="<?php echo "upload/".$_FILES['images']['name']; ?>" width="100" height="100" />
</body>
</html>

I think you don't need 'image_name' field.

Thanks bro. It really works. Is it possible that every time you were upload it will create folder by DAY? And the name of the folder is Month and the day? And the images store to that folder? Is it? If yes, can you give me a sample? Very thanks.

0

Yes of-course it is possible.
try this code before copy command

$folder_name = date('Y-M-d-D');
if(!(is_dir($folder_name)))
mkdir($folder_name);
copy($img_tmp_name,$folder_name."/".$img_name);
0

Yes of-course it is possible.
try this code before copy command

$folder_name = date('Y-M-d-D');
if(!(is_dir($folder_name)))
mkdir($folder_name);
copy($img_tmp_name,$folder_name."/".$img_name);

Thanks bro. It works. I have one more question. How will we are going to do if you are updating the image name, title and description as well as the image? And if you are not going to upload new images the image name will not be change. I mean you update the image title and description only. Can you give me a sample? Thanks bro...

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.