13

I have the following code but I'm trying to shorten it about one or two lines, as I believe the if is unnecessary. Is there any way the code below can shortened to a singular line?

 if(file_exists($myFile))
    {
        $fh = fopen($myFile, 'a');
        fwrite($fh, $message."\n");
    }
    else
    {
        $fh = fopen($myFile, 'w');
        fwrite($fh, $message."\n");
    }
    
2
  • What's the reason for shortening it?
    – Marcus
    Nov 17, 2011 at 10:55
  • 3
    The PHP Manual for fopen clearly state a: "Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it." - so what is the question?
    – Gordon
    Nov 18, 2011 at 9:57

11 Answers 11

66
if (file_exists($myFile)) {
  $fh = fopen($myFile, 'a');
  fwrite($fh, $message."\n");
} else {
  $fh = fopen($myFile, 'w');
  fwrite($fh, $message."\n");
}
fclose($fh);

==

if (file_exists($myFile)) {
  $fh = fopen($myFile, 'a');
} else {
  $fh = fopen($myFile, 'w');
} 
fwrite($fh, $message."\n");
fclose($fh);

==

$fh = fopen($myFile, (file_exists($myFile)) ? 'a' : 'w');
fwrite($fh, $message."\n");
fclose($fh);

== (because a checks if the file exists and creates it if not)

$fh = fopen($myFile, 'a');
fwrite($fh, $message."\n");
fclose($fh);

==

file_put_contents($myFile, $message."\n", FILE_APPEND);

...of course, file_put_contents() is only better if it is the only write you perform on a given handle. If you have any later calls to fwrite() on the same file handle, you're better going with @Pekka's answer.

17

Umm... why? a already does what you need out of the box.

Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

1
  • Good for log. Thx.
    – Kuvalya
    Oct 31, 2020 at 11:14
4
$method = (file_exists($myFile)) ? 'a' : 'w';
$fh = fopen($myFile,$method);
fwrite($fh, $message."\n");
3

fopen(). mode a all you need.

0
3
$fh = (file_exists($myFile)) ? fopen($myFile,'a') : fopen($myFile,'w');
fwrite($fh, $message."\n");
0
2
$fh = file_exists($myFile) ? fopen($myFile, 'a') : fopen($myFile, 'w');
fwrite($fh, $message."\n");
2
  • But why? What sense does the check make in the first place?
    – Pekka
    Nov 17, 2011 at 11:00
  • Your reply makes more sense :)
    – matino
    Nov 17, 2011 at 11:04
1

The append mode already does just what you describe. From the PHP manual page for fopen:

'a': Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

1

According to the php manual this should be enough. See the description of "a"

fopen($myFile, "a");
fwrite($fh, $message."\n");
1
  • 1
    Missing ; after fopen($myFile, "a") Nov 17, 2011 at 11:03
1

I believe the a (append) mode does that already... append if exists, else create new

fopen($myFile, "a");
0
$method = (file_exists($myFile)) ? 'a' : 'w';

$fh = fopen($myFile,$method);

fwrite($fh, $message."\n");

Isn't it $myFile contains absolute/relative path..?

0

Using the SPL / Standard PHP Library:

# addfile.php
$file = new \SplFileObject( __DIR__.'/foo.txt', 'a' );
var_dump( file_exists( $file->getFilename() ) );

$ php addfile.php
bool(true)

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.