php - apache/php 创建文件,但同一页面不允许编辑文件

标签 php linux apache

我在进入我的测试时遇到了一个问题(并在产品上尝试过,不行)我有一个 php 脚本创建一个文件并将一些数据放入该文件中。

我已经使用 winSCP 将“所有者、组、其他”的权限设置为 R、W、X(我还不太了解命令行)。

我能看到的是脚本能够创建文件(我已经删除,并且每次都正确地重新创建文件),但是完全相同的脚本无法写入文件并且我获得了权限错误。 “无法打开流:权限被拒绝”

这是我正在使用的脚本,它在 windows 中工作,但现在在 linux 上,不行。

有什么想法吗?


$type='get';

$count_my_page = ("list".$counterDate.".txt");
if(!file_exists($count_my_page)){
$fp=fopen($count_my_page, "w");
$putArray=array($type=>'1');
$putJson=json_encode($putArray);
fputs($fp, $putJson);
fclose($fp);
} else {

$hits = file($count_my_page);
if(empty($hits)){
    $putArray=array($type=>'1');
$putJson=json_encode($putArray);

} else {
    $putArray=json_decode($hits[0], true);
    if(!array_key_exists($type, $putArray)){
        $putArray[$type] = '1';


    } else {

        $hit=$putArray[$type];

        $putArray[$type]++;

    }
}
$putJson=json_encode($putArray);
$fp=fopen($count_my_page, "w");
fputs($fp, $putJson);
fclose($fp);

}

最佳答案

创建文件需要目录的写权限(这可能是您用 winscp 设置的?)

但是修改文件需要文件本身的写权限。
要为Apache 授予此类权限,您可能必须使用函数chmod。完成文件创建后。

我想这样的事情可能会做:

chmod($count_my_page, 0666);

6 = 4(读)+ 2(写)。
你不需要给执行(1)特权。

这有帮助吗?


我认为它可以在 Windows 上运行,因为 Apache 以您的用户身份 (或管理员) 运行——或者因为 Windows 的权限系统更加宽松


编辑:有关 Linux 下权限的更多详细信息,您可以查看 this part of the corresponding Wikipedia article (引用):

There are three specific permissions on Unix-like systems that apply to each class:

  • The read permission, which grants the ability to read a file. When set for a directory, this permission grants the ability to read the names of files in the directory (but not to find out any further information about them, including file type, size, ownership, permissions, etc.)
  • The write permission, which grants the ability to modify a file. When set for a directory, this permission grants the ability to modify entries in the directory. This includes creating files, deleting files, and renaming files.
  • The execute permission, which grants the ability to execute a file. This permission must be set for executable binaries (for example, a compiled c++ program) or shell scripts (for example, a Perl program) in order to allow the operating system to run them. When set for a directory, this permission grants the ability to traverse its tree in order to access files or subdirectories, but not see files inside the directory (unless read is set).


你也可以玩得开心 umask ,但我一直更喜欢在必要时调用 chmod (并且仅在必要时:我不喜欢授予太多权限——这样更安全)——而 umask 可能会遇到一些问题服务器,如果我没记错的话

关于php - apache/php 创建文件,但同一页面不允许编辑文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1262567/

相关文章:

php - 无法访问不同服务器上的Session变量

php - WordPress .htaccess RewriteRule 不工作

php - MySQL 严格标准警告 - 重新定义类的已定义构造函数

linux - 不使用 ; 执行多个命令和 &?

linux - 如何以嵌套方式使用 va_list 对象,尤其是在 gcc x64 编译器上

php - apache2 + MPM-ITK + PHP = 套接字不起作用

php - WooCommerce - 为免费用户计划添加运费

javascript - AJAX 发布表单不起作用。它绝对没有任何作用

regex - 使用 sed,将特定字符串下方的部分行写入两个不同的变量

php - htaccess force SSL 违反其他规则