1

I have the following code to write to a file.

$ics_file   = "/var/www/Untis/ICS/$ID.ics"; 
if (is_writable($ics_file)) {
  if (!$handle = fopen($ics_file, 'c')) {
     echo "Cannot open file ($ics_file)\n\n";
     exit;
  }
  if (fwrite($handle, $ics_contents) === FALSE) {
    echo "Cannot write to file ($ics_file)\n\n";
    exit;
  }
  fclose($handle);
} else {
  echo "The file <b>$ics_file</b> is not writable\n\n";
}

This code only works if a manually create the file and then chmod it to 777 from the terminal. However I have already made the directory in which it's writing, 777 and I changed the group to www-data.

pi@raspberrypi /var/www/Untis $ ls -all
total 44
drwxrwxrwx 2 pi   www-data 4096 Sep 27 20:49 ICS

However I'm still unable to write in there. The error I'm getting is:

The file /var/www/Untis/ICS/CTE152B.ics is not writable.

I'm currently clueless why its not working for new files, if I chmod the dir to 777 php should be able to write in there right?

Dr. Banana
  • 435
  • 1
  • 7
  • 16
  • Check the permissions of `/var/www/Untis/ICS/`, obviously. Apart from that: you _never_ need `0777`. – arkascha Sep 27 '15 at 21:21
  • 2
    Does the file CTE152B.ics exists before you check is_writable? It will fail if there is no file. You can leave the file name out if you just want to check the directory. – Jason K Sep 27 '15 at 21:31
  • @JasonK Damm, Good catch, I was checking the file instead of the directory. Thanks! – Dr. Banana Sep 28 '15 at 21:57
  • You should get rid of all this useless stuff and leave all these conditions alone. Use just fopen and fwrite. In case of a real error PHP will give you an exact and comprehensive error message instead of your inaccurate homebrewed remarks – Your Common Sense Nov 22 '20 at 08:43

0 Answers0