0

I have an issue that I cannot create any files using fopen.

I am using this as a test:

<?php
$myfile = fopen("testfile.txt", "w")
?>

and this error is output in NewRelic (and no testfile.txt is not created):

fopen(testfile.txt): failed to open stream: Permission denied

GoldenGonaz
  • 1,116
  • 2
  • 17
  • 42
  • 1
    Possible duplicate of [PHP fopen() Error: failed to open stream: Permission denied](http://stackoverflow.com/questions/7665093/php-fopen-error-failed-to-open-stream-permission-denied) – Ruslan Osmanov Nov 19 '16 at 11:08
  • @RuslanOsmanov The duplicate question isn't the same issue I don't think, I am having issues on Linux a server not a local Mac. I'm not able to create the same solution. – GoldenGonaz Nov 19 '16 at 11:15
  • Which user is running the php process? Does that user has permission to create a new file in that location/folder? – cnvzmxcvmcx Nov 19 '16 at 11:18
  • 1
    It is almost the same. The root of the problem is the same: insufficient privileges to write to the file, or to the directory (the current directory, in particular). So check the permissions for both and fix them with `chmod`, e.g. `chmod 775 /path/to/the/directory && chmod 644 /path/to/the/directory/testfile.txt`. Undoubtedly, it's a FAQ. – Ruslan Osmanov Nov 19 '16 at 11:19
  • @GoldenGonaz, look around, there are many links with the same question at the right column "Related". It doesn't matter Mac, or Linux. Both are Unix-like, and the permissions system is the same. – Ruslan Osmanov Nov 19 '16 at 11:26

1 Answers1

1

It's because you don't have the right to open it. Make sure you have the right to write. If on linux you can use chmod() to set the the right of the file.

  • Thanks for the response @user7181041 the file testfile.txt doesn't exist though so I don't know how to set the permissions, I want to create it when I go to my php file (test.php). Only once it's created can I change permissions, but it won't create :) – GoldenGonaz Nov 19 '16 at 11:09
  • if you want to create a file, make sure you have the x right –  Nov 19 '16 at 11:10