38

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?

Kev
  • 118,037
  • 53
  • 300
  • 385
sanders
  • 10,794
  • 27
  • 85
  • 127

4 Answers4

79

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'
Ben Lee
  • 52,489
  • 13
  • 125
  • 145
  • 2
    but when i then print my path on a windows system i get: K:\project345\application/logs/app.log – sanders Nov 14 '10 at 16:10
  • 18
    @sanders, That's not a problem. Windows understands that path. In Windows you can mix forward and backward slashes in a single path. – Ben Lee Nov 14 '10 at 16:16
  • I've seen this being said before but it seems to conflict with the information here: https://stackoverflow.com/questions/2410354/failed-to-open-stream-invalid-argument – Matt Sep 25 '14 at 14:33
  • @Matt, that's a different issue. In that question someone was not escaping backslashes in their string, and `\n` was being converted to a newline. If you are going to use backslashes, you have to escape them properly. That's a basic php issue though, so it's not directly related to this. – Ben Lee Sep 25 '14 at 20:01
  • 1
    Also note that the problem in that question only arose because they were using double-quotes, where `\n` is interpreted that way. If you use single quotes (the the OP did in the example here) then `\n` is not interpreted in any special way, and the literal works fine as-is. – Ben Lee Sep 25 '14 at 20:04
36

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';
Ben Lee
  • 52,489
  • 13
  • 125
  • 145
Qwerty
  • 1,732
  • 1
  • 13
  • 18
1

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

0

Just realpath() is seems to be enough

Example #2

user2928048
  • 3,940
  • 2
  • 12
  • 12