Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

floyde

macrumors 6502a
Original poster
Can I create a unique filename for user uploaded images using PHP's uniqid function? Will it do? or is there a risk of overwriting an exisitng image?

I also found this on the PHP manual, maybe it's less risky:
PHP:
<?php
// no prefix
$token = md5(uniqid());

// better, difficult to guess
$better_token = md5(uniqid(rand(), true));
?>
thanks
 
Could you not just do a check to see if the filename is already used?

You could use

PHP:
file_exists("./path/to/file.jpg")

in an if or while statement to keep creating new names.

Having said that, i am only making this suggestion as i do not know the answer to your previous question! The methods you described would make the possibility of producing the same file name extremely small, but i don't know exactly how small!

Sorry if my answer seems a little vague, but that is the best i have for now!

Good luck with your project,

ac2102
 
Thanks, that will definately work. I'm still wondering if that's the standard way of doing it though.
 
Good thinking with the timestamp. Never used that for filenames, but will now!

Top choice,

ac2102
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.