ok i have this script which uploads a photo to a folder on server and indexes it in database this works fine but i need to change the poto name each time some one uploads found this script but dont know how to incorparate it into my script if any one can advise cheers Doug this is my present upload script <?php $correctAnswer = 4; if($_POST['leg'] != $correctAnswer) { // send em away header("Location: http://www.lostpetsplymouth.com/error-page.html"); exit(); } //This is the directory where images will be saved $target = "uploads/"; $target = $target . basename( $_FILES['photo']['name']); //This gets all the other information from the form $state=$_POST['state']; $type=$_POST['type']; $area=$_POST['area']; $desc=$_POST['desc']; $name=$_POST['name']; $email=$_POST['email']; $tel=$_POST['tel']; $date = date("d-m-Y"); $pname=($_FILES['photo']['name']); // Connects to your Database $dbh=mysql_connect("localhost", "vcd", "57") or die('I cannot connect to database because: ' .mysql_error()) ; mysql_select_db("pets"); //Writes the information to the database mysql_query("INSERT INTO `register` VALUES ('$id', '$state', '$type', '$area', '$desc', '$name', '$email', '$tel', '$pname', '$date')"); //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."; } //send the mail mail('doug@.com','New Record',$msg) ?> <?php echo "your Report has now been added, You will be redirected in three seconds!><br /><br /> <div class='info'>If you don't wish to wait, <a href='index.html'>click here</a>"; echo'<meta http-equiv="REFRESH" content="2;url=index.html">'; ?> PHP: and this is the script i found and want to incorparate <?php //This function separates the extension from the rest of the file name and returns it function findexts ($filename) { $filename = strtolower($filename) ; $exts = split("[/\\.]", $filename) ; $n = count($exts)-1; $exts = $exts[$n]; return $exts; } //This applies the function to our file $ext = findexts ($_FILES['uploaded']['name']) ; //This line assigns a random number to a variable. You could also use a timestamp here if you prefer. $ran = rand () ; //This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended. $ran2 = $ran."."; //This assigns the subdirectory you want to save into... make sure it exists! $target = "uploads/"; //This combines the directory, the random file name, and the extension $target = $target . $ran2.$ext PHP:
Just use rand(), and .= $example = "blah"; $rand1 = rand(1, 1000); $example .= $rand1; Code (markup): This would add a random number from 1 to 1000 at the end of blah.. blah being an example of an image upload name.
You can just add the upload time in unix format to your file name, and you should be fine. To do that just change this line of your code: $target = $target . basename( $_FILES['photo']['name']); PHP: To: $target = $target .time().'_'.basename( $_FILES['photo']['name']); PHP:
ok thanks that worked for renameing the photo but the name that gets put into database did not change do i have to add that line some where else cheers Doug
You have to change $pname along with photo's filename ex if you use: $target = "uploads/"; $target = $target .time().'_'.basename( $_FILES['photo']['name']); Then $pname should be: $pname = time().'_'.($_FILES['photo']['name']);;