1

Using some very simple logging in PHP like the following:

function zppLog($text) {
    $logFilePath = 'my-file-path/log.txt';

    error_log($text, 3, $logFilePath);
}

I.e. I want to store to my own log file.

I'm looking for a way to determine by code where I actually can store the log file into in respect to:

  • Have enough write permissions.
  • Work both on Linux and Windows.

I thought about reading/parsing the PHP.INI but found no way to do this.

My question:

Is there a safe, platform-independent way to get a directory path where my PHP script can write log files into?

(I am not looking for a logging framework, I'm just looking on how to determine a safe folder location)

Update 1:

To clarify: I want to ship the PHP scripts to various customers with different platforms.

So I have no way in advance to do something manual like asking a hoster about a location that is allowed on a certain server.

This has to be a mechanism that will work automatically.

6
  • 1
    "I'm just looking on how to determine a safe folder location" - outside the public html area. $logFilePath = '/var/user/outside_public_html/my-file-path/log.txt'; May 25, 2015 at 19:48
  • @Fred-ii- Are you sure that the /var/user thing will work on a Windows machine?
    – Uwe Keim
    May 25, 2015 at 19:48
  • 1
    yes usually, and hosts will offer a few folders outside the public html realm. You'll need to see what your host has to offer in regards to that, unless you're your own host, then you have complete control over what you can create. May 25, 2015 at 19:50
  • 1
    ok. well, TBH, I am not well-versed in that area in order to provide a solution. Wait for more visitors to the question and maybe adding what you said in your question also. May 25, 2015 at 19:53
  • 1
    I think you should not trust the OS. You can for example write to /tmp/log if the admin left it with default privileges. But then again, service like Selinux can limit you to the defined root directory... May 25, 2015 at 19:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.