Want to edit a sh file using php code

Hi friends,

I want to edit sh file using php, i have tried different method. but its shows permission issues.

Folder path : xyz permission :0755 owner: 545 group: 0
filename : abc.sh permission :0644 owner:0 group: 0

how to edit these files using php/
i have use below codes, but not got any success

$file =$_SERVER['DOCUMENT_ROOT']."xyz/abc.sh";
chmod($file,0777);
exec('chown 545 $file');
exec('chmod 755 $file');

This is not a bug in your program. You genuinely, actually do not have permission to chown and chmod from a webserver because of what your file permissions are. If your webserver doesn't own the files, it can't chmod. And even if it does, you can't chown. Root has to do that.

Can we give permission from root to this file, so we can edit this file using php. we have root access.

It is really not a good idea to run your CGI scripts as root. If someone comprosmises your web server they have won instant access to everything. If someone finds a bug in your program they can exploit it for root access. If your program misbehaves, it can damage anything. There's a reason most things don't run as root, and a reason webservers in particular get very little access to anything -- anyone in the world can cause a request to happen, and can inject any data they want with ease.

You could perhaps use sudo to allow your webserver to run very specific things as root, so you could do system("sudo /path/to/script.sh"); Don't give it a blank cheque. Only let it run very specific scripts, and write those scripts very carefully.