1

I try to delete a file which is in /etc/scripts/

First I created a demo file:

echo "test1234" > test.log

-rw-r--r-- 1 root root 5 Jul 3 13:15 test.log

Now I try to delete it by using PHP:

deleteFile();

function deleteFile()
{
    $file = "/etc/scripts/test.log";

    if (is_file($file)) {
        chmod($file, 0777);
        if (unlink($file)) {
            return "File '$file' deleted.";
        } else {
            return "File '$file' could not be deleted.";
        }
    } else {
        return "$file is not a file!";
    }
}

But I get File '/etc/scripts/test.log' could not be deleted. as response;

I also executed chmod 777 test.log on the file, same result.

5
  • 2
    check your user own the operation or not? This operation might be permitted to the root Commented Jul 3, 2017 at 11:27
  • You probably aren't targeting the file properly, maybe some path issue. Do var_dump(file_exists($file)); first, inside the deleteFile function Commented Jul 3, 2017 at 11:29
  • did you do chmod 777 as root in the console? Doing it from php will not change anything (except if you have managed to get php to run as root) Commented Jul 3, 2017 at 11:32
  • Yes I did it also as root from the shell Commented Jul 3, 2017 at 11:32
  • @samayo, no I checked it by outputting the content with cat, the path is correct Commented Jul 3, 2017 at 11:32

1 Answer 1

3

Not only the file itself, but also the directory /etc/scripts/ should be writable by the user who executes the script.

Sign up to request clarification or add additional context in comments.

1 Comment

You are correct, I had to give the third group write permission, now it works :) But I will do it with shell_exec('sudo rm PATH'); instead and make an entry for that file in the /etc/sudoers, its more secure than allowing public to write.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.