Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP fopen() does not create a file

Tags:

php

fopen

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.

like image 474
Farid Rn Avatar asked Oct 05 '12 12:10

Farid Rn


People also ask

Does fopen create a new file in PHP?

The fopen() function is also used to create a file. Maybe a little confusing, but in PHP, a file is created using the same function used to open files. If you use fopen() on a file that does not exist, it will create it, given that the file is opened for writing (w) or appending (a).

Does fopen create a new file?

The fopen function creates the file if it does not exist.

What does fopen () function do in PHP?

The fopen() function opens a file or URL. Note: When writing to a text file, be sure to use the correct line-ending character! Unix systems use \n, Windows systems use \r\n, and Macintosh systems use \r as the line ending character.


1 Answers

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.

like image 92
Thomas Nordquist Avatar answered Sep 24 '22 02:09

Thomas Nordquist