PHP - Change File Permissions on Linux Server

Hi,

I’ve been reading all over the i-net and could not find a solution to my problem here:

I’m accessing a php script in the backend admin part of my web application VIA WEB BROWSER which tries to delete a file.

I get permission denied errors.

I try to change permissions on the file but it won’t let me.

Here’s my code:

if(is_file($file_path))
{
// change file permissions
chmod($file_path, 0777);

 // delete file
 if(unlink($file_path))
 {
      echo "<p>file deleted !</p>";
 }
 else
 {
      echo "<p>file NOT deleted !</p>";
 }

}
else
{
echo “<p>file does NOT exist !</p>”;
}

Current file permissions are 644.

Any idea what I need to do ?

Thanx in advance for your help !

  • M

Read this http://www.linuxhomenetworking.com/wiki/index.php/Quick_HOWTO_:_Ch20_:_The_Apache_Web_Server#File_Permissions_And_Apache

Quote from the PHP manual on the chmod page (http://www.php.net/manual/en/function.chmod.php)[INDENT]

Note:
The current user is the user under which PHP runs. It is probably not the same user you use for normal shell or FTP access. The mode can be changed only by user who owns the file on most systems.

This also applies to your attempt to delete that file. The current user (the user under which the php process runs) must have rights to delete the file and attempting to set permissions on it will not work if that file isn’t owned by the current user.

What you can do is log in to that server and give the user under which php runs the rights to modify that file, but this might not be possible if you don’t control that server.
[/INDENT]

okay, great idea, how do I find out which user php runs ?

It’s a Virtual Dedicated Server so I do have control…

Thanx.

Hi,

Check this link out http://php.net/manual/en/function.get-current-user.php

Create a simple test script

<?php
echo get_current_user();
?>

and run it in the web browser.