douyun7718
2016-08-25 07:06
浏览 89
已采纳

在Ubuntu中运行PHP程序 - fopen(“filename”,“w”):无法打开流:权限被拒绝

I am using Ubuntu as my OS, and I have XAMPP in it. In the htdocs folder, I have created a project folder in which I have index.php file. Now inside the index.php, a part of the code does this:

fopen("colors_new.csv", "w");

From the manual,

'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

But instead of the file being created, I am getting

Warning: fopen(colors_new.csv): failed to open stream: Permission denied...

And when I look into the project folder, the colors_new.csv file is NOT created.

When I run this program in another computer running Windows OS, it runs successfully. The file is successfully created.

How do I fix this problem in Ubuntu?

WHAT I TRIED:

I tried to run sudo chmod 777 colors_new.csv but I get chmod: can not access colors_new.csv. No such file or directory.. I tried this with just the file name (as shown) while the terminal WAS in the project directory; as well as by giving the full absolute path of the file. Got the same error both times.


NOTE: I have seen other questions, and as indicated above, I tried to give permissions to the file. The other questions did not solve the problem, the accepted answer below did solve it.

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

我使用Ubuntu作为我的操作系统,我有XAMPP。 在 htdocs 文件夹中,我创建了一个项目文件夹,其中包含 index.php 文件。 现在在index.php中,代码的一部分执行此操作:

  fopen(“colors_new.csv”,“w”); 
   
 
 

来自手册

'w'仅供写作开放; 将文件指针放在文件的 开头,并将文件截断为零长度。 如果文件不存在,请尝试创建它。

但是我正在获取<而不是正在创建的文件 / p>

 警告:fopen(colors_new.csv):无法打开流:权限被拒绝... 
   
 
 

< strong>当我查看项目文件夹时,不会创建 colors_new.csv 文件。

当我运行此程序时 在另一台运行Windows操作系统的计算机上,它运行成功 文件已成功创建。

如何在Ubuntu中解决此问题?

我做了什么:

我尝试运行 sudo chmod 777 colors_new.csv ,但我得到 chmod:不能 访问colors_new.csv。 没有这样的文件或目录。 。 我在项目目录中的终端WAS时只用文件名(如图所示)尝试了这个; 以及给出文件的完整绝对路径。 两次都出现相同的错误。


注意:我看到了其他问题,如上所示,我试图授予权限 到文件。 其他问题没有解决问题,下面接受的答案确实解决了。

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

2条回答 默认 最新

  • doulu7921 2016-08-25 07:16
    已采纳

    This means that you don't have the rights to write a file in the directory where your index.php is. A couple of things you can do to make it work

    • give filewriting permissions to the directory. Go with 755 on that and 644 on files, if settings are correct this is sufficient. Never go full 777, which is security risk.

    • create a subdirectory inside your directory and give that sufficient rights (755) to do what you want. F.i. Fopen('./files/colors_new.csv)

    打赏 评论
  • dpbvpgvrhwxen3222 2016-08-25 07:08

    Provide the write permission on the folder in which you want to write the file. Your problem will be solved.

    打赏 评论

相关推荐 更多相似问题