permission denied when doing fopen on a 777 permission file in php

28
2014-06
  • Questioner

    Im not new to php or file permissions. But i just migrated an extremely simple script to a new server I setup.

    The folder which the script runs is in the document root. The permissions for the folder are set to 755.

    The owner of the folder is gsta. The owner of the file is gsta.

    I turned on error_reporting and am getting:

    Warning: fopen(log.txt): failed to open stream: Permission denied in /var/www/html/log.php on line 13
    

    I've tried:

    chmod("log.txt", 0777)
    

    in php and also get permission denied.

    Ive done:

    ps -u | grep httpd
    

    and got:

    gsta      1012  0.0  0.0 108608   808 pts/3    S+   22:45   0:00 grep --color=auto httpd
    

    Code:

    $filename = 'log.txt';
    $handle = fopen($filename,"a+");
    echo "fh:" . fwrite($handle, $payload . "\n") . $handle . "<br/>";
    fclose($handle);
    

    even though it shouldnt matter PHP safemode is off

    I tried setting the owner of the file to nobody (apache often runs under nobody, i also tried setting the owner to apache.

    I have looked at every posting in google and its always the basic things, all of which I have tried. This was a known working script on a different server. I tried checking php.ini looking for some setting that would cause this issue. I have tried deleting the file (as it should create it in that case)

    Does anyone have any ideas? Ive tried everything I can think of.

  • Answers
  • wormhit

    I don't know what it could be, but have your tried creating the file from within your script. just rename the file, make sure fopen is set to read write mode (a should be fine) and have php create it for you.

    If that doesn't work then your folder permissions are wrong, try setting the folder to 777

    also on my box for some reason I have to use two zeros for chmod, 00777

  • lastland

    Is there any possibility that some security stuff, like SELinux, stopped these actions?

    I've run into a similar problem and the command setenforce 0, which would shut down SELinux, successfully resolved it.

  • BugFinder

    "log.txt" are you sure the file you're setting to 777 is the same one? Without a path to it, if the page thinks its running from say the root directory of your site, not the one you're in, you could get errors that way, the Some calls tend to also need the directory to have write permission - I dont think I ever got to the bottom of why some do, some dont.


  • Related Question

    Php having access to the filesystem - getting Permission Denied
  • Hugh

    I'm trying to track down a php issue with a tool that I'm trying to install, and have tracked it down, I believe, to a permissions issue with the filesystem, I believe...

    I now have the following simple .php file:

    <?php
    system("/bin/ls");
    ?>
    

    When I run this, I see the following error in /var/log/httpd/error_log:

    sh: /bin/ls: Permission denied
    

    If I change the php to run:

    <?php
    system("/bin/env");
    ?>
    

    Then I see the output that I was expecting.

    So it's not the execution that doesn't have permissions, it's what the executable is trying to read that is having problems.

    (The tool I am trying to get working here is phplicensewatcher - a tool for keeping an eye on what is going on with various license management daemons)

    Thanks


  • Related Answers
  • John T

    The first two thing I would check:

    • What user is PHP running under? If it's spawned by Apache, it is likely running as "nobody", "www" or "apache" depending on the version and configuration.

    • What directory are you trying to list? Check the permissions on that directory. Does the user PHP is running as have permission to access that directory?