tags:

views:

125

answers:

4

I am working on a php site in which we have to upload images from users.i have to rename that file for preventing conflicts in the name of the image.

uniqid(rand(), true);
and adding a large random number after it.

Will this work perfectly. Any suggestions..??

Its about generation unique names for the image.....

+8  A: 

tempnam() creates a file with a unique name.

FigBug
A: 

You can use Base36 on the AutoIncrement value from a SQL Table (hoping that you do use a SQL table).

$filename = base_convert($last_insert_id, 10, 36);
xubz
A: 

You have two approaches depending on "how" big can be your image library: 1. for a non-big amount of files I do this

<?php

$file = sanitize_file($file); // remove all no [az-09_] characters for safe url linking;
$file_md5 = $md5($file);
$file_extention = $md5($file);

// since I assume the file should belongs to someone you can do this
$file_name = $user_id . $file_md5 . $file_extension;

// then save the file

?>
  1. option.... CacheMogul. Here you need to use your imagination. but for huge amount of files this does a nice sharding so you dont need to worry about a folder max quantity or size
Gabriel Sosa
+1  A: 

Take an md5 of the file and use that. IIRC, the odds of a collision are 1 in 64M. If that's not enough, prefix it with the timestamp expressed in seconds or milliseconds. That way even if a duplicate md5 is generated, the files would have to come in during the same second/millisecond for a collision.

CaseySoftware