Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP exec() command: how to specify working directory?

Tags:

php

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 
like image 611
tputkonen Avatar asked Nov 05 '09 08:11

tputkonen


People also ask

How does exec work in php?

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.

How do you check if exec is enabled in php?

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.

What is the difference between exec and Shell_exec?

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.


2 Answers

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().

like image 147
soulmerge Avatar answered Oct 05 '22 03:10

soulmerge


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().

like image 34
mauris Avatar answered Oct 05 '22 02:10

mauris