Php - change file permissions

I have a PHP script that changes file permissions on my server using chmod. I would like to be able to run the script both through the browser (like no one) and through the cron job (as the username).

Is it right that only the owner of the file can change permissions? That is, if I create a file through a cron job and set permissions, I can’t change these permissions when I run the script from the browser?

Are there any ways around this please? Delete (unlink) and re-create the file like any user the script is working with? Or is there a way to run a PHP script through cron like no one else? / Through a browser as a username?

The goal is to make images public or not by changing file permissions.

+3
source share
5 answers

Solution 1. Create a group for both the user and the cron user, add each user to the new group and give both users read and write access to the file (chmod g + rw filename). (then the following solution is safer).

Solution 2. The easiest way to do this is to make the file readable and writable by everyone (chmod a + rw filename), which will have this effect.

I would not recommend this for use in production.

+2
source

You can do this without putting a username or password in your script.

In your crontab sudo, execute the script as the user your web server is working with. Following your example, I will use the user nobody.

0 12 * * * (sudo -u nobody php./yourscript.php)

, "nobody" ( , "apache" ) . sudo tty. , , : "sudo: , tty sudo"

, "Defaults requiretty", visudo. sudo, , .

+1

, . , .

, SuPHP Apache mod_php. , PHP , script, , PHP , .

(, -), - Joomla FTP. FTP Joomla, FTP. , , FTP.

- ( ):

$ftp = ftp_connect('localhost');
ftp_login($ftp, 'username', 'password');
ftp_chdir($ftp, '/root/to/website');
ftp_chmod($ftp, 0644, 'filename.ext');
ftp_close($ftp);
0

, cronjob "nobody".

-1

( )

-2

Source: https://habr.com/ru/post/1709041/


All Articles