Page 1 of 1

upload and rename help please

Posted: Sat Feb 11, 2012 3:56 pm
by MiniMonty
Hi all,

having a little trouble with upload and rename.
I've got this far:

Code: Select all

$target_path = "images/";
$filename = ( $_FILES['picture']['name']);
// get the file extension
$ext = pathinfo($filename, PATHINFO_EXTENSION);
// strip the extension (assumes .gif, .jpg, .png etc  i.e. 3 letters after a dot
substr($filename, 0, -4);
// rename file with random (timestamp)
$randFilename = uniqid("img_");
$newFilename = $randFilename.".".$ext;
// put file on the server
$target_path = $target_path . basename( $_FILES['picture']['name']); 
if(move_uploaded_file($_FILES['picture']['tmp_name'], $target_path)) {

    echo "The file ".  basename( $_FILES['picture']['name']). 
    " has been uploaded as " . $newFilename;
} else{
    echo "No Pictures uploaded";
}
The script uploads the file but does not rename it - I'm struggling to see what to do next.
All and any help much appreciated

Monty

Re: upload and rename help please

Posted: Sat Feb 11, 2012 8:15 pm
by Celauran
You never make use of $randFilename

Try this small change

Code: Select all

$target_path .= $randFilename;
if(move_uploaded_file($_FILES['picture']['tmp_name'], $target_path))