upload and rename help please

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

upload and rename help please

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: upload and rename help please

Post 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))
Post Reply