php 是否可以执行比此短的“如果文件存在则追加,否则创建新文件”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8165902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:05:47  来源:igfitidea点击:

Is it possible to do "if file exists then append, else create new file" shorter than this

php

提问by david99world

I have the following code but I'm trying to shorten it to be pretty much one or two lines as I'm sure my evaluation of the if is unneeded, is there anyway the code below can be shortened to even a singular line?

我有以下代码,但我试图将其缩短为几乎一两行,因为我确定不需要对 if 的评估,无论如何,下面的代码是否可以缩短为单行?

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

回答by DaveRandom

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 achecks if the file exists and creates it if not)

==(因为a检查文件是否存在,如果不存在则创建它)

$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.

...当然,file_put_contents()只有当它是您对给定句柄执行的唯一写入时才会更好。如果以后fwrite()对同一文件句柄有任何调用,最好使用@Pekka 的回答。

回答by Pekka

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

嗯……为什么? 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.

只开放写作;将文件指针放在文件末尾。如果该文件不存在,请尝试创建它。

回答by jan

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

回答by legiero

fopen(). mode aall you need.

fopen()。模式a所有你需要的。

回答by matino

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

回答by Sonal Khunt

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

回答by Bluewind

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

根据php手册,这应该足够了。参见“a”描述

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

回答by outis

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

追加模式已经完成了您所描述的工作。来自 PHP 手册页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.

'a':只写写;将文件指针放在文件末尾。如果该文件不存在,请尝试创建它。

回答by Pranav Hosangadi

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

我相信a(追加)模式已经做到了......如果存在则追加,否则创建新的

fopen($myFile, "a");

回答by SwR

$method = (file_exists($myFile)) ? 'a' : 'w';

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

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

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

是不是 $myFile 包含绝对/相对路径..?