简体   繁体   中英

PHP - Grant a user permissions to file owned by root in ubuntu

I am using Jasperreports for generating the reports. When I am generating the new reports it will be own by root with permission of 644 . So other users dont have permission to view this report.I want to change the ownership of the file or change the permission.So everyone can view or download the reports.

I tried below php functions

chmod($item, 0777);   

chown($path, 'www-data');

It gives

error: dont have permission to do this

. Because its own by root and current user is www-data. Anyone please help me,

Actually, based on what you're saying, all users have permissions to view that file. 644 means owner can read and write, and group and others can only read. If your script is getting an error reading that file, it might be because of the permissions of the directories in is path, but not the file itself.

If you could change the owner or permissions of a file owned by root like that, it would subvert the whole concept of unix file permissions. Think about it.

You can always change the user running these reports though, or add logic on the report generation side to move or change the permissions on the file as the user who owns it.

As an aside, chmod 777 is an ugly kludge used only by those who have little knowledge of unix permissions . Professionals don't do it. You should bump your understanding of unix file permissions to the next level: https://www.tutorialspoint.com/unix/unix-file-permission.htm looks promising.

According to the manual, the owner and the supersuer have the right to do this.

And you only chage the file mod or owner, will not do. You have also to change the path.

chown

Attempts to change the owner of the file filename to user user. Only the superuser may change the owner of a file.

Note: This function will not work on remote files as the file to be examined must be accessible via the server's filesystem.

Note: When safe mode is enabled, PHP checks whether the files or directories being operated upon have the same UID (owner) as the script that is being executed.

chmod

Attempts to change the mode of the specified file to that given in mode. 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.

Note: This function will not work on remote files as the file to be examined must be accessible via the server's filesystem.

Note: When safe mode is enabled, PHP checks whether the files or directories you are about to operate on have the same UID (owner) as the script that is being executed. In addition, you cannot set the SUID, SGID and sticky bits.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM