Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Folder/File Structure Conventions?

Tags:

php

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.

like image 803
Jamie Redmond Avatar asked Nov 13 '10 13:11

Jamie Redmond


People also ask

What is a naming convention for files and folders?

File naming best practices: File names should be short but descriptive (<25 characters) (Briney, 2015) Avoid special characters or spaces in a file name. Use capitals and underscores instead of periods or spaces or slashes. Use date format ISO 8601: YYYYMMDD.

What is a good folder structure?

One folder structure best practice is to avoid having folders that compete with one another. Try not to create folders with overlapping categories. Instead, create folders which are distinct from one another, and use nesting to arrange them as needed.

How do you describe a folder structure?

A folder structure is the way folders are organized on your computer. As folders are added over time, you can either keep them at the same level—like Folders 1, 2, and 3 in the chart below—or nest them within each other for a hierarchy—like Subfolders 1B and 1B-1 below.


5 Answers

As speshak said PHP has no standards and no conventions (including its own standard library).

However:

  1. In public directory (usually public_html) store only static resources (images/JS/CSS) and one PHP file index.php which is limited to something like this:

    <?php
    
    
    require '/path/to/app/outside/public/html/start.php';
    $app = new App();
    $app->run();
    
  2. Application itself should be stored outside public directory.

  3. File structure might reflect classes names, so: Project\Util\XML\Parser will be stored within /path/to/the/project/Project/Util/XML/Parser.php file.
  4. Of course 3rd-party-code might be stored in separated folder, let's say vendor - that's quite common convention.
like image 106
Crozin Avatar answered Oct 05 '22 04:10

Crozin


There is this:

https://github.com/php-pds/skeleton

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 :-)

like image 26
mindplay.dk Avatar answered Oct 05 '22 05:10

mindplay.dk


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.

like image 43
Silver Light Avatar answered Oct 05 '22 05:10

Silver Light


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 12/56/154.jpg.

You will see many such problems and opportunities over time. But you can look at the current projects and learn from them for free :)

like image 42
Halil Özgür Avatar answered Oct 05 '22 05:10

Halil Özgür


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 :)

like image 44
speshak Avatar answered Oct 05 '22 05:10

speshak