0

I would net your help concerning an issue I'm currently having with PHP

Here is the Code:

<?php

$ics_file   = 'schedule.ics';

if (is_writable($ics_file)) {
    if (!$handle = fopen($ics_file, 'w')) {
        echo "Cannot open file ($ics_file)\n\n";
        exit;
    }

    # Write $ics_contents to opened file
    if (fwrite($handle, "foobar") === FALSE) {
        echo "Cannot write to file ($ics_file)\n\n";
        exit;
    }

    # echo "Success, wrote to <b>schedule.ics</b><br>\n\n";
    fclose($handle);
} else {
    echo "The file <b>$ics_file</b> is not writable\n\n";
}
?>

On my Webpage I always receive "The file schedule.ics is not writable" so it can't access the File. Could you kindly give me a hint into the right direction?

Uwe
  • 41,420
  • 11
  • 90
  • 134
Richard Dana
  • 100
  • 1
  • 6
  • 1
    did you check the permissions on that file? and the permissions of the directory you're in? This has absolutely NOTHING to do with your database, and entirely with your filesystem permissions. – Marc B Jun 13 '14 at 18:19
  • 2
    `mysql_query` is an obsolete interface and should not be used in new applications and will be removed in future versions of PHP. A modern replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/). If you're new to PHP, a guide like [PHP The Right Way](http://www.phptherightway.com/) can help explain best practices. – tadman Jun 13 '14 at 18:37

1 Answers1

1
  1. If file does not exists, is_writable causes your error. You can simply omit this condition and try to write to file and check the result of fopen:

    if ($handle = fopen($ics_file, 'w')) {
        if (fwrite($handle, $ics_contents) === FALSE)
            echo "Cannot write to file ($ics_file)\n\n";
        else {
            # echo "Success, wrote to <b>schedule.ics</b><br>\n\n";
        }
    
        fclose($handle);
        exit;
    }
    else {
        echo "Cannot write to file ($ics_file)\n\n";
        exit;
    }
    
  2. If previous solution does not help, check directory/file permissions. File access depends on server configuration. Try to create file by your own, and set up permission to 777 (for testing). Then script should have access to write into it.

  3. You are reading whole file content to memory. In such case, if you don't need to have the file on disk, you can just send the content to the client using something like this PHP - send file to user.
Community
  • 1
  • 1
Fanda
  • 3,760
  • 5
  • 37
  • 56
  • Thanks for all the quick answers. I checked with your code and indeed it seems I dont have the permission :-/ Now one (maybe very stupid) question, how do I change the permission for the Directory. Haven't used that for quite a while. – Richard Dana Jun 13 '14 at 20:36
  • Just found out that it works with chmod 777 but when I try to do 755 it won't take it – Richard Dana Jun 13 '14 at 20:45
  • Your script probably runs under server user. As I wrote, it depends on configuration of apache, under which user it runs your scripts. You can set permissions by chmod 777 dir-name. – Fanda Jun 13 '14 at 21:17