Page 1 of 1

Best ways to rename documents/images that are uploaded

Posted: Sun Nov 06, 2005 5:12 pm
by raghavan20
I am developing an application where I have to rename documents/images/photos for manageability.
I am looking for logic on which renaming should be done

1. Is use of prefix necessary?
ex: like doc for documents
like img for images
like ph for photos
an example document can be like: doc_004.doc


2. But while generating sequential numbers for naming,,,there might be a collision when two files are uploaded at the same time by different users....what is the normal way to counter it?

I am thinking of adding another prefix which would be the task id/order id of the transaction.
like if it's a document
normal prefix: doc
order id: 1002
so a file would be : doc_1002_01.doc


Guys, if you have got more good logic I would like to hear from you.
How do ebay manage photos uploaded on their server?
Thanks for all your suggestions.

Posted: Sun Nov 06, 2005 5:36 pm
by feyd
This is not a general discussion question. :?

There's nothing that's required (outside of a file) to rename something.. What you rename them to is entirely up to you..

To counter concurrancy you must check for existance of the file you are about to write first, then also gracefully handle an error during moving the file into place...

Posted: Sun Nov 06, 2005 5:49 pm
by raghavan20
If you say to look for a file name before renaming it...then if two users are uploading at the same time and they look at the existance of a file name at the same time which might lead two of them renaming their file names to a same name which would cause collision.

I thought putting a order id as a prefix would be a good idea.

I don't think renaming is a personal preference...cos I am developing a commercial applications very huge volume of documents are to be uploaded. I can not ask the user to rename the file when there is a collision; Instead I am going to rename the file but store the original name of the file in the database for reference under his/her order.

Posted: Sun Nov 06, 2005 5:52 pm
by John Cartwright
How I have handled large amount of requests is assign an ID per request.
The request consists of microtime() (reduced slightly), the user's userid, and the filename..

You seem to already have it worked out..

Posted: Sun Nov 06, 2005 6:41 pm
by shoebappa
Someone's code on the PHP Manual to prevent conflicts http://us2.php.net/features.file-upload:

Code: Select all

//ohdotoh at randomnoisebursts dot com
//10-Oct-2005 12:33 
//my quick and simple don't overwrite files that exist solution: http://us2.php.net/features.file-upload

if (file_exists($sysfolder . '/' . $filename)) {
// append a digit to the beginning of the name
  $tmpVar = 1;
  while(file_exists($sysfolder . '/' . $tmpVar . '-' . $filename)) {
// and keep increasing it until we have a unique name
   $tmpVar++;
   }
  $filename= $tmpVar . '-' . $filename;
  }

Posted: Mon Nov 07, 2005 1:04 am
by n00b Saibot
I have normally used the id suffix method {id}_{no}.{ext} i.e. :arrow: doc_1002_01.doc will work fine & dandy ;)

Posted: Mon Nov 07, 2005 7:32 am
by foobar
Jcart wrote:How I have handled large amount of requests is assign an ID per request.
The request consists of microtime() (reduced slightly), the user's userid, and the filename..

You seem to already have it worked out..
I usually do something similar, except I also MD5 the whole thing into one compact, alphanumerical string.