tags:

views:

80

answers:

4

Hi,

I have an PHP website which creates and stores HTML template files on server based on user input.one user can create many templates.So to store the template files and associate them with the DB record ,what I do is-

"templates" is the table which hold other information about the template such as who created it etc. with unique auto-increment id as template_id

for example - if template id is 1001 I convert it to hex which is 03e9 Now I split the hex number into 03 & e9 (after two numbers) becomes folder and e9 becomes file with some extension as "e9.tpl"

This is how I can find out template from the file system if I know the template ID.I dont need to separately store the path to the file.

is it a good approach ? any shortfalls of this approach ? is there any other approach better than this ?

What are the advantages / disadvantages of storing the path to file in the database itself ? for example to enable using different discs serving templates etc.?

A: 

I don't understand the point in separating the hex into two parts to create different folders... That could create hundreds and hundreds of different folders which would become a complete mess on your server. Why not just store on the templates in one single folder with the hex value as the file name, such as 03e9.tpl?

animuson
+4  A: 

If the ID in the DB table is already UNIQUE, why transform the id for the filesystem at all? Just add a file 1001.tpl and you are all set. If you want to have template files sorted into folders, use the User ID (which I assume to be UNIQUE too), so you get folder 124/1001.tpl.

Depending on your deployment process, you will want to keep the created files outside the application folder, so not accidently delete them when updating the application.

Gordon
+1  A: 

If you have to hit the database to get the id, you may be just as well off storing the template in it as well. But there's nothing categorically wrong with storing them on the file system. I generally would.

When you hit 65,536, you'll get 0x10000. Make sure your code can handle that. I'd be more apt to store 0x1234 like: 1/1234.tpl, just for the sake of clarity. Note that by virtue of sequential IDs, your folders will fill up sequentially.

I'd probably not even convert them to hex. You could use a modulus operator to determine which folder to put them in. Figure out how many files you are likely to have and use that to determine how many folders you want.

For example:

$path = ($id % NUMBER_OF_FOLDERS) . "/$id.tpl"

where $id is the template id in decimal.

konforce
+2  A: 

Are you doing this because you are worried that you might run out of file entries/inodes in the directory? In ext3 the practical limit is somewhere around 100.000 files (and 32.000 dirs).

Creating a directory structure on the fly is better done using modulu as in $dir = $id % 1000 and then put the new template in that dir ($dir/$id.tpl). That strategy will create max 1000 dirs and you have thus made it possible to handle around 100.000.000 files.

I don't see any reason for messing with hexadecimal values or substrings.

Martin Wickman