Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a path work both on linux and Windows

Tags:

linux

php

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?

like image 270
sanders Avatar asked Nov 14 '10 15:11

sanders


People also ask

What is a relative path?

A relative path refers to a location that is relative to a current directory. Relative paths make use of two special symbols, a dot (.) and a double-dot (..), which translate into the current directory and the parent directory. Double dots are used for moving up in the hierarchy.

Which is the correct constant for a platform independent directory separator?

If you prefer to hard-code the directory separator character, you should use the forward slash ( / ) character. It is the only recognized directory separator character on Unix systems, as the output from the example shows, and is the AltDirectorySeparatorChar on Windows.

What are the two paths in Linux?

A relative path is an address relative to the current directory (i.e., the directory in which a user is currently working). An absolute path (also called a full path) is an address relative to the root directory (i.e., the directory at the very top of the filesystem and which contains all other directories and files).

Which is the correct constant for a platform independent directory separator in Python?

In Windows you can use either \ or / as a directory separator.


2 Answers

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' 
like image 199
Ben Lee Avatar answered Sep 21 '22 21:09

Ben Lee


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'; 
like image 28
Qwerty Avatar answered Sep 24 '22 21:09

Qwerty