Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate short filenames for uploaded photos?

Tags:

php

While uploading new photos to a linux server with PHP, I'm having to give them unique names (using it's file name is ruled out). There's no interaction with a DB so getting unique IDs is also not possible.

I thought of using DATE+TIME.jpg, but that's just too long.

So what is the best method to create the shortest possible unique names with PHP?

like image 978
Yeti Avatar asked Feb 25 '10 09:02

Yeti


1 Answers

Use tempnam().

$filename = tempnam  ($dir, $prefix)

It creates a file with a unique name in the specified directory. The generated file name is pretty short and guaranteed to be unique.

like image 157
Martin Wickman Avatar answered Oct 13 '22 12:10

Martin Wickman