0

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.

Jesss
  • 45
  • 1
  • 10
  • 1
    Before running the chmod command you have to at location of file. Otherwise it will throw the error: chmod: can not access colors_new.csv. No such file or directory – Mayank Pandeyz Aug 25 '16 at 07:07
  • 2
    The issue is not with the file since it isn't created but with the folder / directory, you most likely don't have permission to create files in the directory / folder – Epodax Aug 25 '16 at 07:08
  • Possible duplicate of [Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew Aug 27 '16 at 08:52

2 Answers2

2

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

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
2

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)

Pim Broens
  • 702
  • 1
  • 5
  • 16