PHP lesson learnt.. always use the DIRECTORY_SEPARATOR constant

Always use the DIRECTORY_SEPARATOR constant when pointing to files/directories on the server-side. It will make your system more portable.

// Do you usually do this?
require_once dirname( __FILE__ ) . '/bootstrap.php';

// Or this? Don't!
require_once dirname( __FILE__ ) . '\bootstrap.php';

// Do this:
require_once dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'bootstrap.php';

Windows boxes uses backslash as a directory separator while for example Unix uses forward slash. Not using the DIRECTORY_SEPARATOR constant may lead to page-halting errors when for example require() fails locating your file.

Heads up: don’t use it in web URLs :)

This entry was posted in PHP and tagged , , . Bookmark the permalink.
  • Никита Синёв

    Hi, I am working with Denwer server and I have a problem in Firefox browser. While other brosers works Firefox could not find style and js files in which adresses directory_seperator puts backslashes. If I use example number one in your passage everything works good. So what is the problem with backslahses in Firefox ? It writes ‘The requested URL /admin/libsystemstyle.php was not found on this server.’