douya7309
2011-12-28 20:14
浏览 663
已采纳

fopen创建文件,但如何更改权限?

I am creating a new file using fopen.

$filename = 'user_data/10.xml';
$openhandle = fopen($filename, 'w+');

Then I check if the file has been created using: file_exists() function.

The problem is: The file is being created with some owner, probably the folder name, but its not me. Also the permissions of the file is only readable by the owner. And since I am not the owner, I can't read the file, or change the permissions.

But If attempt to change it using:

chown($filename, 'myusername');
chmod($filename, 777);

I tried changing the file owner and permissions using the Terminal using sudo. That worked properly. So I also tried using the functions above with shell_exec() so it runs in root.

But had no luck.

Although, I don't have much experience with file permission numbers, the chown command is also not working.

So how should I change the owner and permissions of the file so i'm the owner and its readable and writable by my other PHP scripts?

图片转代码服务由CSDN问答提供 功能建议

我正在使用fopen创建一个新文件。

  $  filename ='user_data / 10.xml'; 
 $ openhandle = fopen($ filename,'w +'); 
   
 
 

然后我检查文件是否已经 使用: file_exists()函数创建。

问题是:文件是用某个所有者创建的,可能是文件夹名称,但不是我。 此外,文件的权限只有可由所有者读取。 由于我不是所有者,我无法读取文件或更改权限。

但如果尝试使用以下方法更改它:

  chown($ filename,'myusername'); 
chmod($ filename,777); 
    
 
 

我尝试使用 sudo 使用终端更改文件所有者和权限。 这很正常。 我也尝试使用上面的函数与 shell_exec(),所以它在root中运行。

但没有运气。

虽然我对文件权限号没有太多经验,但chown命令也无法正常工作。

那么我应该如何更改所有者和权限 该文件,所以我是所有者,我的其他PHP脚本可读写?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douyang2530 2011-12-28 20:21
    已采纳

    You should be able to chmod it using only the following line:

    chmod($filename, 0777);
    

    Note the 0 before the 777.

    Also do not change the ownership before it has been chmod'ed

    打赏 评论

相关推荐 更多相似问题