我在局域网上有一个Linux文件服务器,并希望能够让不知道如何使用shell的用户搜索文件服务器上的文件。执行输出有时不正确返回什么?
我决定编写一个PHP程序,以便可以从Web浏览器中使用,用户可以在其中放入搜索字符串,并快速列出匹配项。它使用PHP exec函数,并使用Linux find命令。
这对大多数搜索很好,但有时做文本搜索,它什么都没有返回。我调试了这个,看看传递给'find'的东西是什么,并且总是起作用,所以我调用exec和解析输出的方式肯定有问题。但我不知道它是什么。我在php.net上看到了一些关于使用不同方法执行shell命令并将输出返回给PHP程序的讨论,但是我一直没有成功让它在所有情况下都能够正常工作。
我认为这可能是一个Linux文件权限问题,但它在文本搜索中找到的文件与它没有找到的文件具有相同的权限,所有权和组。再次,我知道这些文件在那里,因为如果我自己登录到shell并使用find进行搜索,我可以找到它们。我应该提到,没有shell登录帐户,所以我一直无法'su'到该帐户,并在共享用户的shell中测试我的find命令。
这里是网页PHP网页,后面是.php程序。
<html>
<head>
<link rel="stylesheet" type="text/css" href="search-file-server.css">
<title>Search File Server</title>
</head>
<h1>Search File Server - Version 1.1</h1>
<body>
<TABLE>
<TR>
<TD>Enter all or part of the file name to
<form action="do_search.php" method="post" style="display:inline">search: <input type="text" name="findit">
</TD>
<TR>
<TD>
<input type="submit" name="submit_search">
</form>
</TD>
</TR>
<TR><TD> </TD></TR>
<TR>
<TD>List files and directories created or modified within the last
<form action="do_search.php" method="post" style="display:inline">
<select id="hours" name="hours">
<?php
for ($x = 1; $x <= 24; $x++) {
echo '<option value=' . '"' . $x . '"' . '>' . $x . '</option>';
}
?>
</select>
hour(s).
<TR>
</TD>
<TD>
<input type="submit" name="submit_hours">
</form>
</TD>
</TR>
<TR><TD> </TD></TR>
<TR>
<TD>List files and directories created or modified within the last
<form action="do_search.php" method="post" style="display:inline">
<select id="days" name="days">
<?php
for ($x = 1; $x <= 60; $x++) {
echo '<option value=' . '"' . $x . '"' . '>' . $x . '</option>';
}
?>
</select>
day(s).
<TR>
</TD>
<TD>
<input type="submit" name="submit_days">
</form>
</TD>
</TR>
</TABLE>
</body>
</html>
do_search.php:
<?php
$submit_search = $_POST["submit_search"];
$submit_hours = $_POST["submit_hours"];
$submit_days = $_POST["submit_days"];
$hours = $_POST["hours"];
$days = $_POST["days"];
$findit = $_POST["findit"]; // Search string from user.
if ($submit_search == "Submit") {
echo "<h1>Searching for: " . $findit . "</h1>";
$search_string = '"' . '*' . $findit . '*' . '"';
$find_stuff = "find /home/share -iname " . $search_string . " -not -name " . '".*" ';
exec("$find_stuff",$output);
// Remove the pre-appended directory path from the file server for display purposes.
foreach ($output as $i) {
echo str_replace("/home/share/","",$i) . '<BR>';
}
}
if ($submit_hours == "Submit") {
echo "<h1>Files/Directories created/modified within the last $hours hour(s)</h1>";
// echo "Hours: $hours" . '<BR>';
$minutes = $hours * 60;
$find_stuff = "find /home/share -not -name " . '".*" ' . " -mmin -$minutes";
exec("$find_stuff",$output);
// Remove the pre-appended directory path from the file server for display purposes.
foreach ($output as $i) {
echo str_replace("/home/share/","",$i) . '<BR>';
}
}
if ($submit_days == "Submit") {
echo "<h1>Files/Directories created/modified within the last $days day(s)</h1>";
// echo "Days: $days" . '<BR>';
$find_stuff = "find /home/share -not -name " . '".*" ' . " -mtime -$days";
exec("$find_stuff",$output);
// Remove the pre-appended directory path from the file server for display purposes.
foreach ($output as $i) {
echo str_replace("/home/share/","",$i) . '<BR>';
}
}
?>
</body>
</html>
我不的经营许可在你的php代码中看不到任何可见的问题,这可能是因为执行调用花费太长时间才能完全运行吗?你检查了'max_execution_time'的php设置吗?当搜索中断时,生成的html输出是否不完整? – Calimero
@Calimero感谢您查看代码。不,它立即返回结果或没有结果。这是一致的,如果它没有发现任何东西,它总是找不到完全相同的搜索。 –
好的,那么问题似乎涉及分钟,天,或findit输入字符串? – Calimero