i am working on a website, in which each user can upload multiple images..i am able to upload the image and storing in a folder but i have a problem when two different user have the same image name, as i am storing image in a single folder. In the database where ,i am storing information, serial no is the unique key..i want to rename the image with adding serial no. with the image name to make it unique…please help me..to do it..
my form is as:-
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" /> </br><br/>
<input id="submit" type="submit" value="submit" />
and upload_file.php is as:-
require_once('dbconnect.inc.php');
$db_name="uploaddata";
$tbl_name="data";
$db_selected=mysql_select_db("$db_name")or die("cannot select DB");
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] > 0))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
$Image_name=$_FILES["file"]["name"];
$Image_type=$_FILES["file"]["type"];
//upload is the folder of storing images
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
$_SESSION['response'] = " already exists please give some different name ";
header("Location: testimage.php");
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
$login_data="INSERT INTO $tbl_name(Image_name,Image_type) VALUES('$Image_name','$Image_type')";
$login_result=mysql_query($login_data) or die(mysql_error());
header("Location: abc.php");
}
}
}
You can use move_uploaded_file function to change name of destination file e.g.
You can also use this function