hi i have a simple form where user can insert their name email phone and upload a image.
the form and the php work fine but i wod like my php script to rename the uploaded image to a unique number or naame before saving it to my database and image folder. pls can any one help i am going mad

form script:

<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>

php script:

<?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("mysql6.000webhost.com", "a3205133_test", "sid801142") or die(mysql_error()) ;
mysql_select_db("a3205133_test") 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.";
}
?>

any help wod be good thanx

    I usually generate a random number and prefix it to the image name.

      i usually use the primary key field from the database table as the uploaded file name thereby avoiding any possible file name collisions.

        iam a newbie to php would any one be able to show me what that means. i can manage to get a rondom number to be generated and attached to the uploaded image before saving but not for both places where it gets saved i.e for mysql database and for the image folder.
        sorry if i sound dum

          how do i generate a random number and prefix it to the image name.
          i have tried playing around with .rand() but to no real effect and tried a million other things can some one help and just hand it to me on a plate as i have wasted 4 days trying to sort it out with no effect.
          thankx.

          just clerfy i need to generate a unique number or name to replace the image file name that is uploaded before it gets saved and i need this unique name number to be saved in the database and the image folder with the same unique number.

            use [man]mysql_insert_id[/man] to the primary key from your INSERT query and use that as your file name

              Write a Reply...