Unable to write a file in a directory that my PHP script creates

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ykgoh

    Unable to write a file in a directory that my PHP script creates

    Hi.

    I've a problem of being able to create and remove a directory but
    unable to write a file inside the created directory for some strange
    reason. I suspect that this problem could be vaguely linked to Safe
    mode being set to On since my site is using shared server hosting and
    probably insufficient/incorrect Unix file permission.

    Below is my test script that helps me narrow down the problem.
    --------------------------------------------------------------------------------------------
    <html>
    <head>
    <title>File IO Test</title>
    </head>
    <body>
    <pre>

    <?php
    define("TEST_DI R", "./newsletter/2005_11");
    define("TEST_FI LE", "./newsletter/2005_11/file_io_test.tx t");

    echo "Make Directory";
    if(mkdir(TEST_D IR, 0777)){
    echo "<font color=\"green\" >OK</font>\n";
    }
    else{
    echo "<font color=\"red\">F ail</font>\n";
    }

    if(file_exists( TEST_DIR) && is_dir(TEST_DIR )){
    echo TEST_DIR . " exists.\n";
    }
    else{
    echo TEST_DIR . " does not exists.\n";
    }
    echo "<hr>\n";

    // TEST FILE WRITE
    echo "Write Open File";
    $file_handle = fopen(TEST_FILE , "w");
    if($file_handle ){
    echo "OK";
    }
    else{
    echo "Fail";
    }
    fwrite($file_ha ndle, "This file is written at " . date("Y/m/d H:i:s") .
    "\n");
    echo "Write Close File";
    if(fclose($file _handle)){
    echo "<font color=\"green\" >OK</font>\n";
    }
    else{
    echo "<font color=\"red\">F ail</font>\n";
    }
    echo "--- START FILE DUMP ---\n";
    readfile(TEST_F ILE);
    echo "--- END FILE DUMP ---\n";
    echo "<hr>";
    ?>
    </body>
    </html>
    --------------------------------------------------------------------------------------------
    This script will report a success with the creation of the directory on
    my server, but fails to write a file inside the newly created
    directory.

    Originally, I only call mkdir without specifying any permission and it
    doesn't work. Thus I explicitly instruct mkdir to use 0777 to allow all
    write operation by everybody, in case PHP and Apache try to write files
    using "nobody" or "www" user account, instead of my shell login user
    account. Still it doesn't work. The owner of the created directory is
    called "www". When I login via SSH and check the permission, strangely,
    the permission is 775 instead, with Write permission for Others
    disabled, although I told mkdir to use 0777 instead.

    Is there any workaround, and what am I doing wrong here? Thanks for
    sharing with me any insight into this problem. I'm not quite sure what
    exactly is wrong here, whether because of PHP Safe mode, incorrect
    directory permission, incorrect file permission or the case of
    different UID or owner.

    Goh, Yong Kwang
    Singapore

  • Gordon Burditt

    #2
    Re: Unable to write a file in a directory that my PHP script creates

    >This script will report a success with the creation of the directory on[color=blue]
    >my server, but fails to write a file inside the newly created
    >directory.
    >
    >Originally, I only call mkdir without specifying any permission and it
    >doesn't work. Thus I explicitly instruct mkdir to use 0777 to allow all
    >write operation by everybody, in case PHP and Apache try to write files
    >using "nobody" or "www" user account, instead of my shell login user
    >account. Still it doesn't work. The owner of the created directory is
    >called "www". When I login via SSH and check the permission, strangely,
    >the permission is 775 instead, with Write permission for Others
    >disabled, although I told mkdir to use 0777 instead.[/color]

    Look up "umask". mkdir() and file creation are subject to the
    umask, which by default doesn't allow world write.

    Does the PHP user ("www" in your case) *own* the directory?
    If not, and safe mode is ON, really getting mode 777 won't do any good.

    Gordon L. Burditt

    Comment

    • ykgoh

      #3
      Re: Unable to write a file in a directory that my PHP script creates

      Hi Gordon. I followed your instruction and things seem to be improving
      though not there yet. Basically I set umask(0) before the running mkdir
      and it works.

      Now the directory shows that it has write permission granted to
      everyone. Below is the line I copied after running "ls -la"

      drwxrwxrwx 2 www 84 512 Nov 16 21:16 2005_11

      It shows that the newly created directory is owned by user "www". And
      it has read, write, execute (777) for everyone. But still the file
      write operation fails when it tries a fopen("./2005_11/test.txt", "w")

      What is still wrong I wonder?

      Comment

      Working...