0

When learning about PHP File Create/Write functions I have encountered a problem:

Warning: fopen(newfile.txt): Failed to open stream: Permission denied in /opt/lampp/htdocs/someText.php on line 2
Unable to open file!

The code is very simple:

    <?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "Hello";
fwrite($myfile, $txt);
fclose($myfile);
?>

How to solve this? I tried to get permission when writing various commands on the Terminal, but whatever I wrote, the Terminal said "no such file or directory".
And finally, I found the solution:

sudo chmod 777 /Users/!YOUR USERNAME HERE!/.bitnami/stackman/machines/xampp/volumes/root/htdocs

Maybe it will be helpful to someone :)

Ju Ratė
  • 21
  • 3
  • Did you check permissions on that directory/file. Remember you are not running Apache as your account – RiggsFolly Jan 06 '22 at 13:50
  • I'm sorry, should I do this in the Terminal? I tried to write ls -l and then "chmod 755 opt/lampp/htdocs" but it says "chmod: opt/lampp/htdocs: No such file or directory" – Ju Ratė Jan 06 '22 at 13:56
  • Looks like you forgot a forward slash on that command. `chmod 755 /opt/lampp/htdocs` without the `/` it is relative from executing path. – user3783243 Jan 06 '22 at 13:58
  • You also may want to look into `file_get_contents` and `file_put_contents`. Those, IMO, are a bit easier to use, won't resolve permission issue though. – user3783243 Jan 06 '22 at 13:59
  • Does this answer your question? [PHP fopen() Error: failed to open stream: Permission denied](https://stackoverflow.com/questions/7665093/php-fopen-error-failed-to-open-stream-permission-denied) – Nebojsa Nebojsa Jan 06 '22 at 14:12
  • I read this previous thread about the same problem, but after "sudo chmod 777 newfile.txt", Terminal gives me the answer that "no such file or directory". – Ju Ratė Jan 06 '22 at 14:39
  • Does `ls -lha` show the file you are trying to change? – user3783243 Jan 06 '22 at 14:52
  • I read these similar questions, but the problem is, Terminal doesn't open htdocs and nothing happens. I also tried "sudo chown -R _www:_www /Applications/XAMPP/xamppfiles/htdocs", but it says "nos such file or directory". – Ju Ratė Jan 06 '22 at 15:03
  • You need to find out where your files are located. Sounds like you are in the wrong directories – user3783243 Jan 06 '22 at 15:21
  • I found the solution!!! I wrote this in the Terminal: sudo chmod 777 /Users/-->!YOUR USERNAME HERE! /.bitnami/stackman/machines/xampp/volumes/root/htdocs – Ju Ratė Jan 06 '22 at 15:33
  • @JuRatė That seems like some bitnami image is being used on top of xampp. You should update the question with that, then maybe post your solution as an answer (if question is reopened, I've voted to reopen). – user3783243 Jan 06 '22 at 20:25

0 Answers0