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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

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

Post 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
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

try hard coding the page location?
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

How do I find out the real path to that? when I echoed realpath("../errors/errors.php") I got nothing.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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);
    }
}
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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.
Last edited by Luke on Fri Oct 28, 2005 2:08 pm, edited 1 time in total.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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
Post Reply