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

在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.

  • 写回答

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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决