/    Sign up×
Community /Pin to ProfileBookmark

Forum Post Attachments

Ok, I’m building a forum (I know there are several excellent free, off the shelf, open source forum apps out there, but I’m the kind of guy who wants to build his house rather than buy it ?) and I’m setting it up now to allow attachments to posts. I am trying to figure out the best way of going about this:

I can store the attachment as a file on the server
I can store the attachment as a BLOB in the database

A significant population of developers (of which I am one) think binary information has no place in a database so I am trying to work my way through storing the files on the server but I am encountering a theoretical problem:

Where do I store the attachments so that I can be sure that when one user uploads a file, it won’t overwrite a previous attached file of the same filename?

I was considering storing them in separate folders according to the id of the thread they were posted in, but that doesn’t exclude the possibility of two attachments with the same name in the same thread, then I thought of creating a folder with the unique postid and store the attachment in there, but creating a new folder for every post with an attachment seems like overkill.

How does webdeveloper.com do it? Does anyone have any suggestions?

NOTE: I DO NOT want to alter the file name of the file that was attached in anyway, if I didn’t mind doing that, then this problem would be easy to solve.

Thanks.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@DARTHTAMPONMar 28.2007 — Why cant you rename the files?

anyways you could possably build a folder with $userid/$file for every different user but this sounds like alot of overhead. You could also create say 5 different folders and check to see if the file exists in the first one befor moving it there. If it does go to the 2nd so on and so forth. The only problem with this is if 8 people upload a file with the same name the last 3 either will not get there file stored or someone elses photo may be overwritten depending on how you handle it.
Copy linkTweet thisAlerts:
@NightShift58Mar 29.2007 — When you say that you don't want to rename the file, I assume you mean that you want to preserve the original file name. To prevent duplicates you will have to create directories or rename. Renaming is probably the easiest way as that is already part of the upload process.

You could rename the files in such a way that it would include either a standard prefix (i.e. "username.filename.jpg") to prevent duplicates between users or a standard suffix (i.e. "filename.0001.jpg") to prevent duplicates altogether, or even a combination of both. Separate prefix and/or suffix from the file name with a separator of your choice that you would later use to extract the original file name from the generated one.

If you use this masking technique, you could have your cake and eat it, too. No renaming at all probably means no cake either.
×

Success!

Help @aj_nsc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...