gpt4 book ai didi

php - fopen 创建不存在的新文件

转载 作者:可可西里 更新时间:2023-11-01 11:22:32 24 4
gpt4 key购买 nike

我正在尝试创建一个不存在的文件或写入一个已经存在的文件。

在一个 php 文件中,我正在尝试这样做:

$file = fopen("data.txt", "a");

fwrite($file, "\n" . $name);fwrite($file, "," . $lastname);

fwrite($file, "," . $email);

fclose($file);

我在 Windows Xp 下运行 Apache,但没有创建文件“data.txt”。文档说添加 a 参数应该创建一个文件,其名称在第一个参数 (data.txt) 中提到。我在这里拧干什么?

提前致谢声波

最佳答案

让我们为您的代码添加一些测试和调试输出

echo 'cwd is: ', getcwd(), "<br />\n";
echo 'target should be: ', getcwd(), "/data.txt <br />\n";
echo 'file already exists: ', file_exists('data.txt') ? 'yes':'no', "<br />\n";

$file = fopen("data.txt", "a");
if ( !$file ) {
die('fopen failed');
}
$c = fwrite($file, "\n$name,$lastname,$email");
fclose($file);
echo $c, ' bytes written';

关于php - fopen 创建不存在的新文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2557133/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com