Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP fopen() not creating file if it doesn't already exist

Tags:

As part of application logging, I'm attempting to open a local file, and if that file doesn't already exist, to create the new one. Here's what I have:

$path = '/home/www/phpapp/logs/myawesome_logfile.txt'; $f = (file_exists($path))? fopen($path, "a+") : fopen($path, "w+"); fwrite($f, $msg); fclose($f); chmod($path, 0777); 

I've double-checked, and the /logs directory is chmod 0777, and I even went the extra step of chown'ing it to apache:apache for good measure. Still, when the script goes to open the file, it gives me the warning that the file doesn't exist and bombs out. No file is ever created.

Do I need to suppress the fopen() warning to get it to create the file?

like image 851
b. e. hollenbeck Avatar asked Mar 01 '11 21:03

b. e. hollenbeck


People also ask

Will fopen create a file if not exist?

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

How do you create a file if it doesn't exist in PHP?

PHP Create File - fopen() 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 append create a new file?

Open a binary file in append mode for writing at the end of the 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

When you're working with paths in PHP, the context can matter a great deal. If you're working with urls in a redirection context -- then the root directory ('/') refers to your domain's root. The same goes for paths for linking files or images and for include and require directives.

However, when you're dealing with file system commands such as fopen, the root directory ('/') is the system root. Not your domain root.

To fix this, try giving the full path to the log file you want to open from the system root. For example: /var/www/phpapplication/logs/myLogFile.txt

Or you could use $_SERVER['DOCUMENT_ROOT'] as suggested in other answers to access your server's stored value for the path to the document root. The /var/www part.

You can also use the __DIR__ magic constant in some cases. Note that __DIR__ will be the directory the current file is in, which is not necessarily the same as your application's root. So for example, if your application's root is /var/www/application and you're working in /var/www/application/src/controllers/my_controller.php, then __DIR__ will be /var/www/application/src/controllers. See here in the PHP documentation.

like image 144
Daniel Bingham Avatar answered Sep 24 '22 21:09

Daniel Bingham