fopen throws Permission Denied error

Discussion in 'PHP' started by jj0914, Sep 30, 2008.

  1. #1
    I am using Drupal(php based open source software) to build a website.

    Right now I am experiencing a weird problem. When I use fopen in a test.php and run from command line: "php test.php", I can open file and read/write/append, but if I include that portion of code in side the drupal module I wrote, I get permission denied error.

    Can anyone help me on this? Many thanks
     
    jj0914, Sep 30, 2008 IP
  2. jj0914

    jj0914 Guest

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I re-evalued my problem, i don't think it has anything to do with drupal any more

    the problem now is that I can read a file but not write and append.

    here is the permission of the file I want to open:

    -rwxrwxrwx 1 root root .... pass.txt
     
    jj0914, Sep 30, 2008 IP
  3. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #3
    Maybe show us the fopen code?
     
    Kaizoku, Oct 1, 2008 IP
  4. jj0914

    jj0914 Guest

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    here is the code:

    $file = "/web/website/test.txt";
    $fh = fopen($file, "r");

    $data = fgets($fh);

    print $data.

    now the problem is if I put the text file under /tmp, I can read and append,

    but if I move to another directory, I can't read, write, append. all throw me permission denied error.

    I checked the directory permission, it's 777, and the user and usergroup are apache apache.
     
    jj0914, Oct 1, 2008 IP
  5. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #5
    The problem is you do not use the / from the beginning alone. That indicates that you are using the absolute root. Which is outside of the web folder. Of course it is going to throw an error... use ./ instead of / at the start.
     
    Kaizoku, Oct 1, 2008 IP
  6. jj0914

    jj0914 Guest

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I tried changing / to ./, I got this error

    warning: fopen(./web/files/test.txt) [function.fopen]: failed to open stream: No such file or directory
     
    jj0914, Oct 1, 2008 IP
  7. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #7
    try
    $file = "web/website/test.txt";
    PHP:
     
    ads2help, Oct 1, 2008 IP
  8. jj0914

    jj0914 Guest

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I still get No such file or directory error
     
    jj0914, Oct 1, 2008 IP
  9. jj0914

    jj0914 Guest

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I changed the file name from test.txt to test,

    now I can read the file, but still can't write or append to the file.
     
    jj0914, Oct 1, 2008 IP
  10. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #10
    Ok seems like you are defining the directory wrong. If the file is 1 level deeper use ../ if it's 2 levels ../../
     
    Kaizoku, Oct 1, 2008 IP
  11. dwayne12

    dwayne12 Well-Known Member

    Messages:
    184
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    110
    #11
    It's your file structure. You need the full path to your server home directory.

    For example on my hosting (MediaTemple) I would have to address the file in this way to open it for my domain ProbablySucks.com

    /domains/probablysucks.com/html/test.txt

    I suggest FTP'ing to your directory with FileZilla and copying the server path from the top it shows you.
     
    dwayne12, Oct 1, 2008 IP
  12. Tim-Erwin

    Tim-Erwin Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    jj0914, did you solve your problem? I have the same problem here:

    fopen("/opt/www-data/test.log", "a");

    gives Permission Denied. I checked the permissions on the filesystem, apache is the owner (if not, still everybody has write permission). The path is correct.

    getmyui() and getmygid() give 48 and 48. stat('/opt/www-data/test.log')['uid'|'gid'] same values. I tried using /tmp instead, works. I cannot see why I can't write to /opt.

    Also tried using system("touch /opt/www-data/test") which gives the same issue. On the console with sudo -u apache touch /opt/www-data/test it works.

    Any idea? (As always: works on another system but can't see the difference.)

    Thanks,
    Tim
     
    Tim-Erwin, Nov 6, 2008 IP