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()));
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'];