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

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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.
  <title>File IO Test</title>

define("TEST_DIR", "./newsletter/2005_11");
define("TEST_FILE", "./newsletter/2005_11/file_io_test.txt");

echo "Make Directory";
if(mkdir(TEST_DIR, 0777)){
  echo "<font color=\"green\">OK</font>\n";
  echo "<font color=\"red\">Fail</font>\n";

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

echo "Write Open File";
$file_handle = fopen(TEST_FILE, "w");
  echo "OK";
  echo "Fail";
fwrite($file_handle, "This file is written at " . date("Y/m/d H:i:s") .
echo "Write Close File";
  echo "<font color=\"green\">OK</font>\n";
  echo "<font color=\"red\">Fail</font>\n";
echo "--- START FILE DUMP ---\n";
echo "--- END FILE DUMP ---\n";
echo "<hr>";
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

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

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

Quoted text here. Click to load it

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

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?

Site Timeline