renaming image file before saving to database
Posted: Fri May 15, 2009 4:11 am
hi
i need to make my php rename the uploaded image before saving it.
my form and php work fine but when 2 image files are uploaded with the same file name then onne gets deleted.
pls can some one show how to rename and then save the uploaded image.
many thanks.
form code:
the php script:
i need to make my php rename the uploaded image before saving it.
my form and php work fine but when 2 image files are uploaded with the same file name then onne gets deleted.
pls can some one show how to rename and then save the uploaded image.
many thanks.
form code:
Code: Select all
<html>
<head>
<title></title>
</head><body>[/color]<form enctype="multipart/form-data" action="add.php" method="post">
Name: <input name="name" type="text"><br>
E-mail: <input name="email" type="text"><br>
Phone: <input name="phone" type="text"><br>
Photo: <input name="photo" type="file"><br>
<input value="Add" type="submit">
</form></body></html>
the php script:
Code: Select all
<?php
//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$pic=($_FILES['photo']['name']);
// Connects to your Database
mysql_connect("", "", "") or die(mysql_error()) ;
mysql_select_db("") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO `employees` VALUES ('id','$name', '$email', '$phone', '$pic')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>