I have a PHP file on a web-host that is as follows:
<?php
file_put_contents('test.txt','TEST');
echo 'OK';
?>
But the file test.txt
is not created when I execute the page from a browser. Why not? Is it a permissions issue?
I have a PHP file on a web-host that is as follows:
<?php
file_put_contents('test.txt','TEST');
echo 'OK';
?>
But the file test.txt
is not created when I execute the page from a browser. Why not? Is it a permissions issue?
是的,PHP Doc 说
如果文件名不存在,则创建文件。否则,现有文件将被覆盖,除非设置了 FILE_APPEND 标志。
仅当您写入对该文件夹具有权限时....尝试
error_reporting(E_ALL);
ini_set("display_errors", "on");
if (! is_writable(__DIR__)) {
trigger_error("I don't have permission");
}
file_put_contents('test.txt', 'TEST');
为什么不?是权限问题吗?
你应该问自己的问题是你为什么不知道?
代码将产生错误或警告 - 你为什么不知道那是什么?
这可能是权限问题 - 权限是什么?
顺便说一句:在您的文档根目录中写入内容到将执行 PHP 的目录是一个巨大的安全漏洞。即如果是权限问题,那么更改权限是解决问题的错误方法。