【发布时间】:2016-07-16 09:31:21
【问题描述】:
下面获取一个字符串并检查服务器上的所有文件(./*)以查看该字符串是否存在于任何文件中,然后将数据输出到> grep_results.txt:
$command = 'grep -ri "any string goes here" ./* > grep_results.txt';
shell_exec($command);
无论如何,如果可能的话,我想通过以下 html 表单提交的变量传递给它:
<form id='searchform' action='/GREP.php' method='post'>
<div class='form-group'>
<input type='text' name='SearchString' placeholder='Search string...'>
<input type='submit' name='submit' value='Find' class='button'>
</div>
</form>
我开始这样做了:
$string = $_POST['SearchString'];
echo $string;
当然,任何提交的字符串都会被回显。
卡住的地方是,我试过了,还是不行:
$string = $_POST['SearchString'];
$command = 'grep -ri "'.$string.'" ./* > grep_results.txt';
shell_exec($command);
如何将 $string 传递给 $command?
【问题讨论】:
-
当您从浏览器调用时,顶部的代码(带有硬编码字符串)是否已经正常工作?
grep_results.txt必须可由 Web 服务器用户写入才能正常工作,并且您绝对必须在$string上调用escapeshellarg(),然后再将其传递给$command。 php.net/manual/en/function.escapeshellarg.php