Vishnu
Vishnu

Reputation: 461

fopen failes even if the script, the parent folder and the target file, all are owned by same user

Even if safe mode is activated on the server, if script file, target file and parent folder is owned by the same user, fopen should work without any problem. Any ideas why this is happening? The files have 755 permission. Thank you for your time and effort.

$filename="file.html";
echo "Current User: ".get_current_user()."; UID: ".getmyuid()."; GID: ".getmygid()."<br/>";
echo "Current PID: ". getmypid() . "<br/>";
echo "Parent folder owner: ". fileowner(".") . "<br/>";
echo "$filename owner: " . fileowner($filename) . "<br/>";
echo "Current folder is writable: " . is_writable($filename);

OUTPUT:

Current User: vnnamp; UID: 32024; GID: 32026 Parent folder owner: 32024 file.html owner: 32024 Current folder is writable: false

Upvotes: 1

Views: 367

Answers (3)

JohnKlehm
JohnKlehm

Reputation: 2398

Are you sure your script is operating in the correct directory?

If it is not then the file name you pass it likely doesn't exist and that would be why is_writable() returns false. Use getcwd() to print out the directory the script is operating in (can set it with chdir()) or use absolute file names.

EDIT: Just to summarize the discussion below:

  • Try absolute path name: Nope
  • Check open-basedir in php.ini and httpd.conf: Nope
  • Try prefixing path with $_SERVER['DOCUMENT_ROOT']: Nope
  • getmyuid() returns script file owner not process owner check mismatch file owner and process user with posix_getpwuid(posix_geteuid()): Yes, cause of the problem

Upvotes: 2

nullpotent
nullpotent

Reputation: 9260

The result of is_writable() function is cached. Try clearing it with clearstatcache()

Upvotes: 0

mephisto123
mephisto123

Reputation: 1448

If the user is the owner of the file, it does not mean file should be writable by the user. It may have permissions like 0444 or so.

Upvotes: 0

Related Questions