Page 1 of 1

Rename uploaded file

Posted: Wed Nov 04, 2009 5:34 pm
by Niall
Hola! I'm fairly new to this whole PHP business, and I'm creating a file uploader for some experience / giggles.

So far I've got it working pretty well, however I'm wondering about how to rename the file once uploaded.
As for generating, I've gone pretty basic and just done current time + random number between 1 and 100. Simple enough.

What I'm doing is once the file is succesfully uploaded, renameUpload(); then move_uploaded_file etc.
This is the function in all it's glory:

Code: Select all

function renameUpload($file) {
    $rand = mt_rand(100, 999);
    $time = time();
    $ext = strstr($file,'.');
    $filenew = $rand.$time.$ext;
    rename($file, $filenew);
}
And once the upload is successful:

Code: Select all

else {
        renameUpload($name);
        move_uploaded_file($temp,"files/".$name);
        echo $success;
    }
$success is basically a string saying it uploaded successfully.

The problem is, when I run it I get this error:

Warning: rename(clark!.fla,1941257376894.fla) [function.rename]: No such file or directory in C:\xampp\htdocs\swfshire\upload.php on line 46

I'm not quite sure why.
Any suggestions? I'm pretty lost here, and most likely missing a trick.

Re: Rename uploaded file

Posted: Wed Nov 04, 2009 6:58 pm
by Eric!
Does "clark!.fla" exist in C:\xampp\htdocs\swfshire\? My guess is it doesn't.

Re: Rename uploaded file

Posted: Thu Nov 05, 2009 10:59 am
by Niall
Oh, WHOOPS HAHA


Thanks for pointing that out, dunno how I missed that. I've got it working now.