Hi all,
I am rather new to PHP, but have a fairly ok grasp of software design. My question arises from the fact that I'm getting ready to deploy some scripts etc. to my website which is hosted on a *nix type server. My home / work computer that I'm developing on is windows. My apache server is: Apache HTTP Server Version 2.2 running on my windows XP sp2 desktop. So when I port all of my php code over, do I have to changeall of my directory slashes from whacks to slashes when I do that?

Hi EggMatters,

To make your code work on either Windows or *nix platforms, use the constant PATH_SEPARATOR in place of a slash. For example:

$stuff = file_get_contents("some_dir". PATH_SEPARATOR."another_dir". PATH_SEPARATOR.$filename);

The constant PATH_SEPARATOR represents "/" on *nix systems and "\" on Windows systems.

Hi EggMatters,

To make your code work on either Windows or *nix platforms, use the constant DIRECTORY_SEPARATOR in place of a slash. For example:

$stuff = file_get_contents("some_dir". DIRECTORY_SEPARATOR."another_dir". DIRECTORY_SEPARATOR.$filename);

The constant DIRECTORY_SEPARATOR represents "/" on *nix systems and "\" on Windows systems.

Excellent suggestion. Thanks!

a nice little trick I have been shown is to defne a shorter constant for DIRECTORY_SEPARATOR.

<?PHP
   defined('DIRECTORY_SEPARATOR') ? null : define('DS', DIRECTORY_SEPARATOR); 
?>

you dont need to do the defined part its just incase DS is later defined as PHP standard constant.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.