我在局域網上有一個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