My script, let's call it execute.php, needs to start a shell script which is in Scripts subfolder. Script has to be executed so, that its working directory is Scripts. How to accomplish this simple task in PHP?
Directory structure looks like this:
execute.php Scripts/ script.sh
The exec() function is an inbuilt function in PHP which is used to execute an external program and returns the last line of the output. It also returns NULL if no command run properly.
php phpinfo(); ?> You can search for disable_functions and if exec is listed it means it is disabled. To enable it just remove the exec from the line and then you need to restart Apache and you will be good to go. If exec is not listed in the disable_functions line it means that it is enabled.
1. The shell_exec() function is an inbuilt function in PHP that is used to execute the commands via shell and return the complete output as a string. The exec() function is an inbuilt function in PHP that is used to execute an external program and returns the last line of the output.
Either you change to that directory within the exec command (exec("cd Scripts && ./script.sh")
) or you change the working directory of the PHP process using chdir()
.
The current working directory is the same as the PHP script's current working directory.
Simply use chdir()
to change the working directory before you exec()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With