sanders
sanders

Reputation: 10888

Make a path work both on linux and Windows

How can I make sure that this path:

new Zend_Log_Writer_Stream(APPLICATION_PATH . '\logs\app.log')  

works both on linux and on windows?

Upvotes: 39

Views: 63706

Answers (4)

Qwerty
Qwerty

Reputation: 1742

You can also use DIRECTORY_SEPARATOR constant instead of \ or /. Usually you'll want to redefine it to have shorter name, like

define('DS', DIRECTORY_SEPARATOR);
$filename = APP . DS . 'logs' . DS . 'file.txt';

Upvotes: 36

user2928048
user2928048

Reputation: 4118

Just realpath() is seems to be enough

Example #2

Upvotes: 0

Edwin Irausquin
Edwin Irausquin

Reputation: 21

if you want to communicate two or more app of your site, this trick will serve you much

$ Document_root = realpath ( \ filter_input ( INPUT_SERVER , ' DOCUMENT_ROOT '));

this is to convert the route you back real path and then just have to navigate between directories with the DIRECTORY_SEPARATOR without worrying about the operating system installed on your machine or web server

Upvotes: 1

Ben Lee
Ben Lee

Reputation: 53309

In Linux, the path separator is /. In Windows, it is either \ or /. So just use forward slashes and you will be fine.

APPLICATION_PATH . '/logs/app.log'

Upvotes: 81

Related Questions