0

This works:

$logFile = fopen("".$user.".log", "w") or die("Unable to open log file for {$user}!");

But this does not:

$logFile = fopen("logs/".$user.".log", "w") or die("Unable to open log file for {$user}!");

I'm on a linux machine. My logs directory exists in the same directory as my index.php. I've tried a forward slash before logs directory. I've tried the absolute path. I've tried literal forward slashes, double slashes, triple slashes...running out of ideas here.

cursesNOOB
  • 11
  • 6
  • What is the error you get? Is it a permissions issue? Using "or die" is not the best practice - very uninformative. Would suggest exception handling (and/or `file_exists`). – ficuscr Aug 25 '20 at 18:00
  • Where can I see errors? Also, the file doesn't exist. I am creating it. – cursesNOOB Aug 25 '20 at 18:11
  • [How do I get PHP errors to display?](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) – ficuscr Aug 25 '20 at 18:15
  • (Replacing sensitive info with ***) Warning: fopen(/logs/****.log): failed to open stream: No such file or directory in /share/CACHEDEV1_DATA/Web/index.php on line 463 Unable to open log file for *****! Why is it trying to open file in some weird CACHEDEV folder? It is working as intended if i don't specify a directory :( – cursesNOOB Aug 25 '20 at 18:22
  • Ahh, some progress... Warning: fopen(logs/****.log): failed to open stream: Permission denied in /share/CACHEDEV1_DATA/Web/index.php on line 463 Unable to open log file for ****! It's a matter of permissions? how could I write fine without specifying directory but breaks when i make a directory within the local scope? – cursesNOOB Aug 25 '20 at 18:27
  • Because the directory you create has different permissions? As for the path... would be something I hope you know - I can't tell from here. QNAP? Some other NAS? Beats me. – ficuscr Aug 25 '20 at 18:30
  • Does this answer your question? [Permission denied when opening or creating files with PHP](https://stackoverflow.com/questions/9200557/permission-denied-when-opening-or-creating-files-with-php) – ficuscr Aug 25 '20 at 18:32
  • Not really but now it works after a chmod 0777 – cursesNOOB Aug 25 '20 at 18:46
  • Read up on linux file permissions. See `umask`. – ficuscr Aug 25 '20 at 18:49
  • Do you know wich permissions I should use? I just want my index.php file to be able to write to files within this directory. My website is not online, just used by ppl on the LAN – cursesNOOB Aug 25 '20 at 18:52
  • You don't want the code writing to the web root, why you create a folder under it called `logs` or `upload` or something and give it write permissions. So use `chmod -R 750 /share/CACHEDEV1_DATA/Web` and then `chmod -R 770 [...]/Web/logs`. I like to use a www-data group for web folder (same group as apache). Can then optionally use `chmod g+s -R [...]/web/logs` – ficuscr Aug 25 '20 at 19:08

0 Answers0