tweetok tweetok - 3 months ago 18
PHP Question

Unique file name after uploading

I'm trying to rename a image after uploading it.
I want it to be a unique name.

I have this class (found on the internet):

http://pastebin.com/qQmC256A

I tried editing the file like this:

$upload_image = $target_path.basename($fileName);


to

$upload_image = $target_path.basename(uniqid($fileName, rand()));


but it gives me just a file, without an extension..

Answer

What else should happen? uniqid will generate something like:

php > echo uniqid('kittens.jpg', rand());
kittens.jpg57b72004c1a470.87520799
php > echo basename(uniqid('kittens.jpg', rand()));
kittens.jpg57b7201cdce2e9.40812361

You don't get a extension, because you turned the file's extension into part of the "middle" of the filename.

Perhaps something like this would work better:

$info = pathinfo($file);
$file = $path . $info['filename'] . uniqid() . $info['extension'];
Comments