Basic File Structure

July 7th, 2008

A lot of times when I do work for companies, or freelance work, I really stress the importance of good file structuring so programmers, and non-programmers, know the flow of the web site. I am sure there are a few of you rolling your eyes already thinking this is a waste saying, “I already know how to do this”, but then there are some saying, “This is great, just what I need to help me get started in web site developing”. This article is for that audience. However, some of you who already know how to structure a web site could learn something new.

A lot of you might structure the web site like so:

  • index.php
  • db_access.php
  • footer.php
  • header.php
  • images
    • (all the images)
  • style.css

or

  • index.php
  • include
    • db_access.php
    • footer.php
    • header.php
    • style.css
  • images
    • (all the images)

If you are new to web site development, the first structure is most likely your method, placing all your files in the root directory without knowing the importance or hierarchy of the files and folders. The second method is approached more often once you become more experienced and start organizing your files and folders. These two methods are fine and expected, but I would like to share with you how I organize my files and folders so maybe you can start learning at a more quicker pace. This is how I structure my web sites:

  • assets
    • assets.php
    • css
      • (all css files)
    • images
      • (all image files)
    • js
      • (all javascript files)
    • php
      • (all php files)
  • index.php
  • layout.php

I have a directory called “assets” (of course you can call this “include” if you wish), and within that assets directory I have more directories that correspond to what that specific file/extension belongs to. So CSS files go in the “css” directory, PHP classes and functions go in the “php” directory, etc. My “assets.php” file keeps track of all my main and global variables. For example, I like to create a variable called $DR (short for Document Root), which acts like a fake document root, yet works just like a document root. This is very helpful for when trying to place an image on the page. Instead of having to guess how many directories I have to think back, I can just do this:

[php]

[/php]

I could just type / instead of ../../../../, but again, I use the $DR as a fake document root because maybe I have the site in separate directory. Ok, lets get back on track, apologizes for straying a bit. In my assets.php file, I could also use PHP’s build in function session_start() so I don’t have to keep typing it every time, and have it connect to my MySQL database, etc.

Well, that is all I have to share with my Basic File Structure. Please let me know how you structure your websites, I would love to hear some tips and tricks from your experience. Thanks

Website

Leave a Reply

Enter code snippets inside of <pre> and </pre>