php写入文件并设置权限

发布于 2024-10-20 21:12:38 字数 451 浏览 1 评论 0原文

我正在尝试创建一个 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 技术交流群。

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

发布评论

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

评论(3

软的没边 2024-10-27 21:12:38

是的,您可以感谢 PHP CHMOD

// Read and write for owner, read for everybody else
chmod("/somedir/somefile", 0644);

Yes, you can thanks to PHP CHMOD

// Read and write for owner, read for everybody else
chmod("/somedir/somefile", 0644);
小鸟爱天空丶 2024-10-27 21:12:38

由于之前的答案中没有涵盖这方面,我将在此处添加它:

chmod() 只会将路径字符串作为第一个参数。因此,您不能尝试传递到使用 fopen() 打开的资源,在本例中为 $fh

您需要 fclose() 资源,然后使用文件路径运行 chmod()。因此,正确的做法是将 filePath 存储在变量中,并在调用 fopen() 时使用该变量,而不是在第一个参数中直接传递字符串。

对于答案中的示例代码,这仅意味着运行 chmod($myfile, 0755) (权限代码只是一个示例,当然会有所不同。)

更正后的完整代码:

<?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);


// Here comes the added chmod:
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 with fopen(), in this case $fh.

You need to fclose() the resource and then run chmod() with the file path. So a proper practice would be storing the filePath in a variable and using that variable when calling fopen() 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

$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);


// Here comes the added chmod:
chmod($myFile, 0755);
?>
非要怀念 2024-10-27 21:12:38

Php 有 chmod,与 Linux 版本一样工作。

Php has chmod, works just like the Linux version.

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