Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to organize files created dynamically using php?

Tags:

php

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.?

like image 780
Sourabh Avatar asked Mar 18 '26 14:03

Sourabh


2 Answers

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.

like image 97
Gordon Avatar answered Mar 20 '26 09:03

Gordon


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.

like image 45
Martin Wickman Avatar answered Mar 20 '26 09:03

Martin Wickman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!