By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,082 Members | 2,108 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,082 IT Pros & Developers. It's quick & easy.

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

P: n/a
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_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";
}
else{
echo "<font color=\"red\">Fail</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_handle, "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\">Fail</font>\n";
}
echo "--- START FILE DUMP ---\n";
readfile(TEST_FILE);
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

Nov 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
>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.


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
Nov 22 '05 #2

P: n/a
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?

Nov 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.