php写入文件并设置权限
我正在尝试创建一个 php 文件,我可以立即编辑该文件,而无需手动设置权限。
我正在尝试这个...
<?php
$var = '<?php $mycontent = new Content(); echo $mycontent->block($p_name);?>';
$myFile = "testFile.php";
$fh = fopen($myFile, 'w+') or die("can't open file");
$stringData = $var;
fwrite($fh, $stringData);
fclose($fh);
?>
...它创建了该文件,但是当我尝试在 IDE 中编辑该文件时,它当然不会让我这样做。我必须手动设置创建的文件的权限。有什么方法可以创建该文件并已设置权限吗?
预先感谢
毛罗
I'm trying to create a php file which I can edit straight away without manually set the permissions.
I'm trying this...
<?php
$var = '<?php $mycontent = new Content(); echo $mycontent->block($p_name);?>';
$myFile = "testFile.php";
$fh = fopen($myFile, 'w+') or die("can't open file");
$stringData = $var;
fwrite($fh, $stringData);
fclose($fh);
?>
...it creates the file, but when I try to edit the file in my IDE it won't let me of course. I have to manually set the permission of the file created. Is there any way I can create the file and have the permission already set?
Thanks in advance
Mauro
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,您可以感谢 PHP CHMOD
Yes, you can thanks to PHP CHMOD
由于之前的答案中没有涵盖这方面,我将在此处添加它:
chmod()
只会将路径字符串作为第一个参数。因此,您不能尝试传递到使用fopen()
打开的资源,在本例中为$fh
。您需要
fclose()
资源,然后使用文件路径运行chmod()
。因此,正确的做法是将 filePath 存储在变量中,并在调用 fopen() 时使用该变量,而不是在第一个参数中直接传递字符串。对于答案中的示例代码,这仅意味着运行 chmod($myfile, 0755) (权限代码只是一个示例,当然会有所不同。)
更正后的完整代码:
Since this aspect wasn't covered in previous answers I'll add it here:
chmod()
will only take a path string as the 1st argument. So you cannot try to pass to resource that was open withfopen()
, in this case$fh
.You need to
fclose()
the resource and then runchmod()
with the file path. So a proper practice would be storing the filePath in a variable and using that variable when callingfopen()
rather than passing it a direct string in the first argument.In the case of the example code in the answer this would simply mean running
chmod($myfile, 0755)
(the permission code is only an example and be different of course.)full code after corrections:
Php 有 chmod,与 Linux 版本一样工作。
Php has chmod, works just like the Linux version.