Running a php script to delete something

So I exec 'rm -rf sth' from a php script run by /usr/bin/php5 /path/to/phpscript.php.

sth is a directory with the status:

drwxr-sr-x 2 www-data www-data 4096 Jan 11 12:58 sth

An everything in it has the status:

-rw-r–r-- 1 www-data www-data 5189 Jan 11 12:58 blahblahblah

The phpscript.php status is:

-rw-r–r-- 1 myuser www-data 544 Jan 10 07:00 phpscript.php

Everytime I try to run /usr/bin/php5 /path/to/phpscript.php, it fails to delete sth and gives me permission denied error:

rm: cannot remove `/path/to/sth': Permission denied

How can I make it work? I tried to change the ownership of phpscript.php to that of sth, but it didn't work.

5 Replies

Did you try adding sudo before rm?

Who's executing the script? Does that person have rights to remove that directory?

> Did you try adding sudo before rm?

Wouldn't that be prompting you for password? The rm command is executed in a php script via exec(). I don't know if I can do that.

> Who's executing the script? Does that person have rights to remove that directory?

So the person who's executing the script should have the permissions to delete the thing rather than the owner of the script?

So www-data should be the person who executes the script instead of me (myuser)?

@mwalling:

Who's executing the script? Does that person have rights to remove that directory?

That's a great insight. I've added a crontab entry for user www-data instead of my user myuser:

sudo crontab -e -u www-data

Will see if it works.

The script owner doesn't change who the script runs as (unless you use magical incantations (sticky bits)).

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct