tweetok tweetok - 1 year ago 91
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):

I tried editing the file like this:

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


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

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

Answer Source

What else should happen? uniqid will generate something like:

php > echo uniqid('kittens.jpg', rand());
php > echo basename(uniqid('kittens.jpg', rand()));

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'];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download