Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ensuring unique names

Status
Not open for further replies.

kpdvx

Programmer
Dec 1, 2001
87
US
I am building an online art gallery for a friend. It will have maybe 15 users uploading their art. I had planned on every artists art being uploaded to artwork/ but somehow i need to keep the filenames unique. I was thinking of $filename = $userid.$random_number.$filename, but would this ensure unique filenames? Or should I have each user have their own directory within artwork/? Can this be done? Could I have it create a directory upon user signup? And if I could do this... could I have a file written to that directory (such as index.php)? Will this be possible (and secure)?
 
u can have a column id which will be auto increment,
then u can retrieve the max id each time user uploads the file, add one to it and then append it to the filename.

but this can raise some concurrancy issues..

spookie --------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
What about this?
$rstring = substr(md5(time()),0,8);
$filename = $userid.$rstring.'_'.$filename;

? Would this always be unique? I would think it would be because time is always unique...
 
The time is always unique? You never have two users using the site at the same time?

I'd do what rosenk suggested, use tempnam() to get a unique name. //Daniel
 
Auto-increment in a database? an encode date-time?

I use date("dmy-Gis") in some code that makes PDF's --BB
 
These people answered your question, and answered it well... but for the sake of argument...

what about giving each user their own directory and going from there? Then it would be trivial to guarantee unique filenames through some method or another, and you can manage things a little easier... or if you are limited to one directory, do something like append the time and the username to the file.

-Rob
 
Anything other than using [tt]tempnam()[/tt] or using a unique identifier from a database (with appropriate use of locking depending on the specific database in use) is leaving open the potential for a conflict during simultaneous access.

You can reduce the risk by doing things like giving each user their own directory, but what about the case of the same user being connected twice? You can add a timestamp to the file name, but what about two users submitting at exactly the same time (to the precision of the time you decide to use.)

[tt]tempnam()[/tt] is designed just for this purpose. The only negative I can really see to using it is that you do not have a whole lot of control over the resulting filename.
 
Is there any way to create a directory by script? As in when a user creates an account, a directory is automatically created? If I could do this, I would use fwrite to make an index.php file...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top