[英]Cant run shell script in PHP
当我尝试使用exec
和shell_exe
运行shell脚本时,什么也没发生! 当我使用这些命令ls
或whoami
运行所有命令时。
会是什么呢?
您是否回显输出?
echo exec('ls');
您是否启用了safe_mode?
phpinfo();
是的话:(从手册中)
注意:启用安全模式后,您只能在safe_mode_exec_dir中执行文件。 出于实际原因,当前不允许在可执行文件的路径中包含..组件。
尝试使用以下命令调用exec
exec('...pathtoyourbashscript...', $out, $return);
然后
echo $return;
如果显示127
,则可能是路径错误。
同时检查权限。 用户“ nobody”可能是apache用户,该用户需要访问和执行脚本的权限。
您可以通过运行来更改权限
chmod 755 pathtouyourscript
这意味着类似“我不介意其他人是否读取或运行此文件,但只有我才能修改它”。
如果您使用的是Apache,请检查以确保Apache用户具有执行php文件所需的权限。
您可以使用反射来确定是否已使用disable_functions
禁用了该功能。
$exec = new ReflectionFunction('exec');
print $exec->isDisabled() ? 'Disabled' : 'Enabled';
如果程序是基于Web的,即针对linux,请尝试制作一个php文件来处理外壳。 和一个用于处理php的外壳文件。
例如:runAllShell.php文件可以包含一个循环。
<?php
// Developed by A T.
// Count the files
$directory = '/put/directory/path_here/';
$files = glob($directory . '*.*');
if ( $files !== false )
{
$filecounter = count( $files );
}
else
{
echo "No Files were found";
}
$fileCount = $filecounter;
// Start the index
$fileNumber = 1;
while($fileNumber <= fileCount){
shell_exec('$fileNumber . ".sh"');
// get some feedback
echo "The ".$fileNumber." file has been excecuted";
// increment file number
$fileNumber++;
}
?>
确保目录中所有.sh文件的编号均按数字顺序执行,例如:1.sh 2.sh 3.sh等。
最好的问候,AT。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.