Link to home
Start Free TrialLog in
Avatar of Richard Lloyd
Richard Lloyd

asked on

PHP fwrite to windows absolute path

I have various websites that share directories on  windows server, running PHP7.2.

The structure is:

c:\websites\domain1.com\app -> Admin website
c:\websites\domain1.com\clients\clientAdomain.com -> Client A website
c:\websites\domain1.com\clients\clientBdomain.com ->  Client B website
c:\websites\domain1.com\clients\clientCdomain.com ->  Client C website
etc

Each website has subfolders

Any scripts that are shared amongst all the folders throughout all the websites are stored in c:\websites\common and "included" where necessary


I also have a "logs" folder at
c:\websites\logs

I am trying to find a way of using fwrite with an absolute path to always write any logs to the logs folder, from wherever the scripts is called from. Ideally I'd like to use something like

fopen("c:\websites\logs\log.txt","+a");

But I don't think that it works.

Any advice would be appreciated
Avatar of arnold
arnold
Flag of United States of America image

You are running apache rather than iis?
Usually each instance logs to its own stracture.
It is isolating each site

Presumably, your php has a database back end you could use access to the db, though it will add to the resource consumption.
ASKER CERTIFIED SOLUTION
Avatar of Brian Tao
Brian Tao
Flag of Taiwan, Province of China image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In C type languages the '\' is an escape character it means use the '\' in combination with the next character to define another (usually non-printable) character.
So \t (tab) \n (newline / linefeed), \r (carriage return)
When you need to use the '\' as a backslash then escape it with another '\'
C:\\path

Open in new window


Personally I don't like defining constant strings inside the function that uses them - makes maintaining them a pain.
Recommendation:
In your common / config file
define('LOGPATH', "c:\\websites\\logs\\");

Open in new window


Then in your use case
fopen(LOGPATH . "log.txt","+a");

Open in new window

Avatar of Richard Lloyd
Richard Lloyd

ASKER

Thanks

The double escaping got me!