|
Posted by ykgoh on 11/15/05 20:24
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
Navigation:
[Reply to this message]
|