PHP Application Development Part One - Directory structure (
Page 2 of 6 )
Directory structure and filesystem naming conventions are something that rarely receive enough attention in PHP applications. It is common practice to simply begin work and save all project files to a publicly accessible Web directory, despite the fact that many PHP files, especially configuration files and files uploaded through the application itself, never need to be directly accessed via the Web. It is also common to see files of all types – configuration files, function libraries, class files, logs, and so forth all mixed in the main application directory or all stuffed into one subdirectory.
In addition to this, files of all kinds tend to have haphazard names that often do not adequately convey the purpose of the file – a problem that is compounded when the files do not live in a directory that conveys the general purpose of a group of files. As a project grows, something seemingly insignificant like a bit of filesystem disorganization can become a maintenance nightmare.
It is important to organize your project files, period. Directories should have names that are meaningful and should be in the appropriate location. The image below is an example of what a basic PHP Web application directory structure should look like. Images used in this article will be captured in Windows but do not rely on any operating system specific features or system directories.

In this example, “public_html” is the Web accessible directory and “webapp” is our project directory. On Red Hat this might be “/home/webapp/”. The “public_html” directory (the default name for the web directory in Red Hat) will contain files a user would directly access in a Web browser, such as “index.php”. In this directory you would organize your css files, javascript files, and images into the appropriate directories. These files must be in the Web accessible directory since browsers need access to them.
The parent directory, “webapp”, is where all files that do not need to be accessed by a Web browser will reside. The “classes” directory will be used to store class files, the “config” directory will store configuration files, “lib” will store files containing function libraries, “logs” will contain our applications custom log files, and “templates” will be the directory where we store our HTML template files.
The names of these directories are clear and meaningful and reside in the proper locations. In a product intended for repeat use, files outside of the public Web directory would be stored in a centralized location on the server and accessed by project scripts at that location either directly or by creating symbolic links (shortcuts in Windows) to those directories.
There are some simple security considerations when planning your Web applications directory structure, one of which we addressed above – keeping files out of the Web accessible directory if they aren’t accessed via the Web. Other simple considerations include preventing directory listings, blocking hotlinking, and password protecting areas that require a login through .htaccess files (or IIS Virtual Directory configuration). Authentication through .htaccess files uses basic HTTP authentication and is only one possible method of requiring authentication on a directory.
| | Discuss PHP Application Development Part One | | | | | | | I really like this article. It provides a lot of great information on good PHP... | | | | | | I will respectfully disagree with your comments =) PHP is a remarkably fast... | | | | | | Nice work! I always love to see articles relating to coding practices. Good luck on... | | | | | | Perhaps that is where my true disagreement is. I for one think:
"She has a... | | | | | | I will be mentioning templates later in the series and discussing why you should... | | | | | | Extreme example but compare the following two - find $chartype in each:
"Not only... | | | | | | All right, you've convinced me. Placing the variables outside of the string is... | | | | | | 0verall, this was a great article for covering basic PHP coding conventions. The... | | | | | | Thanks for the reply. I agree that using underscores is an acceptable convention,... | | | | | | In your article you stated;
------
<?php
$obj = &new MyObject();
... | | | | | | Somehow I did forget to mention that was fixed in PHP 5 - could've sworn I had.... | | | | | | Right. Good article.
I've noted a small mistake though:
<quote>
for ($i = 0;... | | | | | | It was my understanding that unless an object contained lots of data, such as a huge... | | | | | | ...I just wish I'd read it a year ago. It's comforting other people have to refer... | | | | | | On a similar note, I used to think while() loops were more efficient than foreach()... | | | | | | I did tests on this about a year ago and got some pretty dramatic results. I'm sure... | | | | | | Thanks for pointing that out, I'll make sure it's fixed. I promise I know the right... | | | | | | I think you meant "comma", not "colon", when you stated:
the correct spelling... | | | | | |
$myNumber = 2;
$myNumberSquared = Math::square($myNumber);
[b] print... | | | | | | Right!
Thank god I don't right articles... | | | | | | Yes, I like the underscore better for file names, because when you copy files... | | | | | | Just my two cents:
I've read somewhere that this:
echo 'Welcome back ',... | | | | | | I knew what he meant, the correction has already been made. Thanks =) | | | | | | I agree with satane and the previous poster; _ is my prefered method. I come from a... | | | | | | Your example, "did i name it myDb or myDB", implies a lack of agreed upon naming... | | | | | | What methods do you use to benchmark your PHP applications? Thanks.
Ben | | | | | | Not true. Passing echo parameters is slower than concatenation. I've done timing... | | | | | | You could do this in at least a few ways...
Use a load generator to check the... | | | | | | [quote]placing variables outside of strings increases readability[/quote]
I think... | | | | | | >>> Post your comment now! | | | | | |
|
 |