Pit Digger
Pit Digger

Reputation: 9780

PHP Write File Error

I have a page called index.php which is calling a function "writelog" in includes/Logger.php

I have file located at includes folder and code is as below.

function writelog($logText){
            $myFile = "testlog.txt";
            $fh = fopen($myFile, 'w') or die("can't open file");
            $stringData = $logText + "\n";
            fwrite($fh, $stringData);
            fclose($fh);
}

It shows errror "can't open file" . I have set FullPermission for everyone and still it says it cant access file.I tried to put file in same folder as index.php and same error. What can be possible cause ? Am I having wrong path ?

Upvotes: 0

Views: 131

Answers (2)

Ravi
Ravi

Reputation: 737

I am assuming this file is also in includes, I'm guessing this is called from another script so the path would be one of the calling script. You can use this: $prevdir = getcwd(); chdir(dirname(__FILE__)); $myFile = "testlog.txt"; chdir($prevdir);

But it's best to use absolute paths

Upvotes: 0

Trevor
Trevor

Reputation: 6689

Try using the full path of the log file

$myFile = "/full/path/to/testlog.txt";

Upvotes: 1

Related Questions