Page 1 of 1

Saving to files that aren't in the root directory.

Posted: Tue Oct 25, 2005 11:18 am
by Luke
I am trying to log to files that are above the root directory... how come this won't work?

Code: Select all

function logerror($error){
    $date = time();
    $file = "../errors/errors.php";
    if($fh = fopen($file, "a")){
        $newerror = date("h:i:s Y/m/d", $date)."
Error: $error
Page: ".$_SERVER['PHP_SELF']."

";
        fwrite($fh, $newerror);
        fclose($fh);
    }
}
ERROR:
Warning: fopen(../errors/errors.php): failed to open stream: No such file or directory in /home/pdirect/public_html/includes/functions.php on line 21

Posted: Tue Oct 25, 2005 11:22 am
by Charles256
try hard coding the page location?

Posted: Tue Oct 25, 2005 11:29 am
by Maugrim_The_Reaper
echo

realpath("../errors/errors.php");

to screen and ensure its matching your full path. Using realpath() itself might work - symbolic links can be a bit awkward on some systems.

Posted: Tue Oct 25, 2005 11:44 am
by Luke
How do I find out the real path to that? when I echoed realpath("../errors/errors.php") I got nothing.

Posted: Tue Oct 25, 2005 11:46 am
by Maugrim_The_Reaper
/home/pdirect/public_html/includes/functions.php

from the error message? ;)

You shouldn't really post full paths to forums - just a little advice ;) But there's your answer in full view - test using the full path and it'll work for sure.

Posted: Tue Oct 25, 2005 11:52 am
by hawleyjr
I recommend getting the path by using

Code: Select all

dirname($_SERVER['DOCUMENT_ROOT']).'/includes/';
This way if you change your server you won't have to change the path in your code.

Posted: Tue Oct 25, 2005 12:11 pm
by Jenk
The path he actually wants is:

/home/pdirect/public_html/errors/errors.php

Change your function to:

Code: Select all

function logerror($error){
    $date = time();
    $file = realpath("../errors/errors.php");
    if($fh = fopen($file, "a")){
        $newerror = date("h:i:s Y/m/d", $date)."
Error: $error
Page: ".$_SERVER['PHP_SELF']."

";
        fwrite($fh, $newerror);
        fclose($fh);
    }
}

Posted: Fri Oct 28, 2005 2:01 pm
by Luke

Code: Select all

dirname($_SERVER['DOCUMENT_ROOT'])
did not work... and the new server I am working on is windows which I think makes things weird... look at the path in the error message:
Error wrote:e:\Inetpub\wwwroot\admin\news\index.php
I work with about 10 diff servers and this one is windows.

Posted: Fri Oct 28, 2005 2:08 pm
by Luke

Code: Select all

include(realpath("../functions.php"));
include(realpath("../classes.php"));
Did not work either:
Warning: main() [function.include]: Failed opening '' for inclusion (include_path='.;C:\php5\pear') in e:\Inetpub\wwwroot\admin\news\index.php on line 2

Warning: main() [function.include]: Failed opening '' for inclusion (include_path='.;C:\php5\pear') in e:\Inetpub\wwwroot\admin\news\index.php on line 3