4

According to fopen documentation, in some modes it will create the file if it does not exist, but in my situation, I've checked all 'w', 'w+', 'x' and 'x+' modes but it's just throwing warnings at me and it cannot create the file.

It's my code:

$this->handle = fopen($this->log_name, 'w');

and what I get:

Warning: fopen(D:\xampp\htdocs\farid\logs\error.php) [function.fopen]: failed to open stream: No such file or directory in D:\xampp\htdocs\farid\libraries\error\log.php on line 34
Warning: fwrite() expects parameter 1 to be resource, boolean given in D:\xampp\htdocs\farid\libraries\error\log.php on line 66
Warning: fclose() expects parameter 1 to be resource, boolean given in D:\xampp\htdocs\farid\libraries\error\log.php on line 27

And I'm working in windows environment.

9
  • 3
    File permissions is the most obvious suspect Commented Oct 5, 2012 at 12:47
  • 1
    From the same documentation - "If PHP has decided that filename specifies a local file, then it will try to open a stream on that file. The file must be accessible to PHP, so you need to ensure that the file access permissions allow this access. If you have enabled safe mode, or open_basedir further restrictions may apply." Commented Oct 5, 2012 at 12:47
  • Can you add more code? Maybe it's going wrong 1-2 line above your current code. Commented Oct 5, 2012 at 12:48
  • First the folder has to exist already. If it does, make sure it is writable. Commented Oct 5, 2012 at 12:50
  • @anyber Yes, Directory exists Commented Oct 5, 2012 at 12:56

1 Answer 1

8

Check if the path to the logfile exists, it creates a file, not a directory.

Also check if the user, which runs xampp, has access to the folder you specified.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your help, there was a mistake in my directory name. now it's creating file. Is there any way so I even create the folder if it didn't exist in my fuunction?
check out php.net/manual/en/function.mkdir.php check the argument section look for "recursive", first you have to check if the file exists, realpath and and file_exists could be helpful
"it creates a file, not a directory" <-- indeed, that was it. Had to manually add a folder.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.