0

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..

tweetok
  • 70
  • 1
  • 1
  • 9

2 Answers2

0

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'];
Marc B
  • 356,200
  • 43
  • 426
  • 500
0

On line 26 of the library you provided there's a variable named $file_ext that contain exactly what you need. Just concat it to the $upload_image so line 32 becomes:

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

Even if, as stated here, I would get the extension in this way: $path = $_FILES['image']['name']; $file_ext = pathinfo($path, PATHINFO_EXTENSION);

Because if the file name is, for example, test.tar.gz it will take only .gz as extension.

If you want to use this method, you could substitute line 22-24 with the above code.

Community
  • 1
  • 1
Giulio
  • 1
  • 1
  • 1