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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化