Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:创建不带chmod()的只读文件_Php_Chmod - Fatal编程技术网

PHP:创建不带chmod()的只读文件

PHP:创建不带chmod()的只读文件,php,chmod,Php,Chmod,我已经在我的服务器上禁用了chmod(),但我仍然希望用户能够通过PHP创建只读文件,有办法吗?您不能,因为无法将任何内容放入只读文件中 编辑实际上,有一种方法: <?php $u = umask(0377); // disables --wxrwxrwx permissions $f = fopen("test", "w"); umask($u); fwrite($f, "this is a test\n"); fclose($f); ?> % php foo

我已经在我的服务器上禁用了chmod(),但我仍然希望用户能够通过PHP创建只读文件,有办法吗?

您不能,因为无法将任何内容放入只读文件中

编辑实际上,有一种方法:

<?php
  $u = umask(0377); // disables --wxrwxrwx permissions
  $f = fopen("test", "w");
  umask($u);
  fwrite($f, "this is a test\n");
  fclose($f);
?>

% php foo.php
% ls -l test
-r--------  1 xxx xxx  14 19 May 10:27 test
% cat test
this is a test

%php foo.php
%ls-l试验
-r------1 xxx xxx 14 5月19日10:27测试
%猫试验
这是一个测试

操作允许您创建一个读/写文件描述符,即使底层目录项是只读的。

您不能,因为不可能在只读文件中放入任何内容

编辑实际上,有一种方法:

<?php
  $u = umask(0377); // disables --wxrwxrwx permissions
  $f = fopen("test", "w");
  umask($u);
  fwrite($f, "this is a test\n");
  fclose($f);
?>

% php foo.php
% ls -l test
-r--------  1 xxx xxx  14 19 May 10:27 test
% cat test
this is a test

%php foo.php
%ls-l试验
-r------1 xxx xxx 14 5月19日10:27测试
%猫试验
这是一个测试

操作允许您创建读/写文件描述符,即使基础目录条目是只读的。

作为解决方法,您可以先使用存储文件,然后使用更改文件权限


另一个选项是通过命令行,但如果禁用了
chmod
,则
exec
也不太可能工作。如果你的主人同时禁用了这两种功能,那么很可能是有原因的。(如果只是为了降低支持成本。)

作为解决方法,您可以先使用存储文件,然后使用更改文件权限


另一个选项是通过命令行,但如果禁用了
chmod
,则
exec
也不太可能工作。如果你的主人同时禁用了这两种功能,那么很可能是有原因的。(如果只是为了降低支持成本。)

对于Alnitak以外的任何人来说:如果你很难理解umask值(就像我一样),请记住,创建权限位文件时使用的是umask的倒数(并在倒数后删除执行位)。因此,有了这个umask,新创建的文件将是0400(或者-r-------如果你愿意的话)。对Alnitak以外的任何人来说:如果你的大脑很难理解umask值(就像我的一样),请记住创建文件的权限位是umask的倒数(并在倒数之后删除执行位)。因此,使用该umask,新创建的文件将是0400(或者-r-------如果您愿意的话)。