I’m trying to find a guide on PHP file/folder structure conventions.
I’m using GitHub and want to ensure I’m following a standard convention as to not confuse users.
Any help would be appreciated.
As speshak said PHP has no standards and no conventions (including its own standard library).
In public directory (usually
public_html) store only static resources (images/JS/CSS) and one PHP file
index.phpwhich is limited to something like this:
<?php require '/path/to/app/outside/public/html/start.php'; $app = new App(); $app->run();
Application itself should be stored outside public directory.
- File structure might reflect classes names, so:
Project\Util\XML\Parserwill be stored within
- Of course 3rd-party-code might be stored in separated folder, let’s say
vendor– that’s quite common convention.
There is this:
Based on data collected from various open-source projects, this document proposes a naming convention for a number of root-level directories designated for various common purposes, as well as naming conventions for root-level documentation files.
I didn’t find that this covers every imaginable scenario, but it’s nice to be able to point at this and indicate you’re making an effort to achieve some consistency across projects 🙂
You are free to choose any directory structure. But if you would like to know about best practices, take a look at how it is done in frameworks, for example, symphony.
Take a look: http://www.flickr.com/photos/deia/402335716/
Here’s few of them:
All the code that is included, should be put outside of the document root.
HTML templates should be in a separate directory.
Libraries and classes should be in ‘lib’ directory.
Mostly it’s just a reasonable solutions, not strict conventions.
I think the most obvious one is your libraries. You should name your classes like
YourCompany_Module_Class, if you want to be compatible. Using this standard, your libraries can be used along with any other similarly named libraries without clashes and problems. The new namespacing in PHP 5.3+ helps more in achieving this. You can have some guidelines for this at Zend Coding Standards – File Naming and at PSR-0 Standard Proposal.
Other than that, you’d better constantly keep the future in your mind and plan your folder structure accordingly. For example, let’s say you are uploading images into
user_images. Ok. But what happens when the project catches or gets bigger and now you have tens of thousands of files in a single folder. You must build some scheme that enables you to store ~1k images per directory at most like
You will see many such problems and opportunities over time. But you can look at the current projects and learn from them for free 🙂
PHP really has no standard. If you are making use of some framework (eg CakePHP, Zend Framework, etc) it may impose some standard on you.
If you aren’t using a third party library that forces a structure, just use common sense. (Put images in a images directory, included files in an includes directory, etc) People that download and install PHP apps will already be used to each app doing things differently. The fact that you’re giving it some thought puts you a head of lots of the competition 🙂