Best ways to rename documents/images that are uploaded

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Best ways to rename documents/images that are uploaded

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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...
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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..
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post 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;
  }
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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 ;)
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post 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.
Post Reply