PHP exec() 命令:如何指定工作目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1679045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP exec() command: how to specify working directory?
提问by tputkonen
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?
我的脚本,我们称之为execute.php,需要启动一个位于Scripts 子文件夹中的shell 脚本。脚本必须被执行,所以它的工作目录是 Scripts。如何在 PHP 中完成这个简单的任务?
Directory structure looks like this:
目录结构如下所示:
execute.php
Scripts/
script.sh
回答by soulmerge
回答by mauris
The current working directory is the same as the PHP script's current working directory.
当前工作目录与 PHP 脚本的当前工作目录相同。
Simply use chdir()to change the working directory before you exec().
只需chdir()在您之前使用更改工作目录exec()。
回答by Inshallah
For greater control over how the child process will be executed, you can use the proc_open()function:
为了更好地控制子进程的执行方式,您可以使用proc_open()函数:
$cmd = 'Scripts/script.sh';
$cwd = 'Scripts';
$spec = array(
// can something more portable be passed here instead of /dev/null?
0 => array('file', '/dev/null', 'r'),
1 => array('file', '/dev/null', 'w'),
2 => array('file', '/dev/null', 'w'),
);
$ph = proc_open($cmd, $spec, $pipes, $cwd);
if ($ph === FALSE) {
// open error
}
// If we are not passing /dev/null like above, we should close
// our ends of any pipes to signal that we're done. Otherwise
// the call to proc_close below may block indefinitely.
foreach ($pipes as $pipe) {
@fclose($pipe);
}
// will wait for the process to terminate
$exit_code = proc_close($ph);
if ($exit_code !== 0) {
// child error
}
回答by timdev
If you really need your working directory to be scripts, try:
如果您确实需要将工作目录作为脚本,请尝试:
exec('cd /path/to/scripts; ./script.sh');
Otherwise,
除此以外,
exec('/path/to/scripts/script.sh');
should suffice.
应该足够了。
回答by Ben
This is NOT the best way :
这不是最好的方法:
exec('cd /patto/scripts; ./script.sh');
exec('cd /patto/scripts; ./script.sh');
Passing this to the exec function will always execute ./scripts.sh, which could lead to the script not being executed with the right working directory if the cdcommand fails.
将此传递给 exec 函数将始终执行 ./scripts.sh,如果cd命令失败,这可能会导致脚本无法使用正确的工作目录执行。
Do this instead :
这样做:
exec('cd /patto/scripts && ./script.sh');
exec('cd /patto/scripts && ./script.sh');
&&is the AND logical operator. With this operator the script will only be executed if the cdcommand is successful.
&&是 AND 逻辑运算符。使用此操作符,只有在cd命令成功时才会执行脚本。
This is a trick that uses the way shells optimize expression evaluation : since this is an AND operation, if the left part does not evaluate to TRUE then there is not way the whole expression can evaluate to TRUE, so the shells won't event process the right part of the expression.
这是一个使用 shell 优化表达式计算方式的技巧:由于这是一个 AND 运算,如果左侧部分的计算结果不为 TRUE,那么整个表达式无法计算为 TRUE,因此 shell 不会进行事件处理表达式的正确部分。

