fopen 创建不存在的新文件

发布于 2024-08-27 03:45:38 字数 364 浏览 7 评论 0原文

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

在 php 文件中我尝试这样做:

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

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

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

fclose($文件);

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

提前致谢 低音

I am trying to either create a file that doesn't exist or write to a file that already does.

within a php file I am trying this:

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

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

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

fclose($file);

I am running Apache under windows Xp and have no luck that the file "data.txt" is being created. The docs say that adding the a parameter should create a file with a name mentioned in the fist parameter (data.txt). what am I doing wring here?

Thanks in advance
undersound

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

妄想挽回 2024-09-03 03:45:38

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

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';

Let's add some tests and debug output to your code

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';
仅此而已 2024-09-03 03:45:38

您是否正在查看脚本也认为它是当前目录的目录?你的apache进程有写权限吗?错误日志中是否提到了任何失败的命令?

Are you looking into that directory the script also considers it's current one? Does your apache process have write permission there? Does the error log mention any failing command in this?

只为一人 2024-09-03 03:45:38

请记住,该文件是在进程主线程运行的目录中创建的。这意味着如果“主”程序位于 A/ 中,并且它包含来自 B/ 的函数文件,则 B 中的函数会在“.”中创建文件。在A/而不是B/中创建它

另外,不需要调用fwrite函数3次,你可以这样做

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

Remember that the file is created in the directory where the main thread of the process is running. This meas that if the "main" program is in A/ and it includes a functions file from B/ a function in B that creates a file in "." creates it in A/ not in B/

Also, there's no need to call the fwrite function 3 times, you can do

fwrite($file, "\n" . $name . ", " . $lastname . ", " . $email);
地狱即天堂 2024-09-03 03:45:38

如果附加到已存在的 data.txt 有效,但未创建尚不存在的文件,则很可能意味着文件权限允许写入该文件,但该文件所在的文件夹不属于 Apache,并且因此,它无法在那里创建文件。

If appending to an already existing data.txt works, but not creating a file that does not yet exist, it would most likely mean that the files permissions allow writing into it, but the folder the file resides in is not owned by Apache, and as such, it cannot create files there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文