私はLAN上にLinuxファイルサーバーを持っており、ファイルサーバー上のファイルを検索するために、シェルの使い方を知らないユーザーがいるようにしたいと考えていました。実行時出力が間違って何も返さない場合がありますか?
PHPプログラムを書くことに決めたので、ユーザーが検索文字列を入力できるWebブラウザから使用できるようになり、一致するものがすぐに一覧表示されます。これはPHPのexec関数を使用し、Linuxのfindコマンドを使用します。
これはほとんどの検索でうまくいきますが、テキスト検索を行うと何も返されません。私はこれをデバッグして、何が 'find'に渡されているかを確認しています。常に動作します。したがって、execを呼び出して出力を解析する方法に何か問題があるはずです。しかし、私はそれが何であるか分からない。私は、php.netで、シェルコマンドを実行して出力をPHPプログラムに戻すためのさまざまな方法の使用についていくつか議論しましたが、すべての状況で常に動作するようには成功しませんでした。
Linuxのファイルアクセス権の問題だと思っていましたが、テキスト検索で見つかったファイルは、見つからないものと同じアクセス権、所有権とグループを持っています。再び、私はそれらのファイルがあることを知っています。なぜなら、シェルに自分自身でログインし、findを使って検索すると、それらを見つけることができるからです。私は言及すべきことは、シェルのログインアカウントがないので、私はそのアカウントに 'su'することができず、共有ユーザーとしてシェルでfindコマンドをテストすることができなかった。
ここにはWebのPHP Webページがあり、その後に.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>
Iドンの許可を持っているディレクトリを必要とSamba共有ユーザーとして動作していたので、PHPでのexecコマンドは、$出力を返すことができなかった理由が説明
あなたのPHPコードに目に見える問題がないか、execコールが完全に実行するには時間がかかりすぎるかもしれませんか?あなたは 'max_execution_time' PHP設定をチェックしましたか?検索が中断した場合、結果として得られるHTML出力は不完全ですか? – Calimero
@Calimeroコードを探していただきありがとうございます。いいえ、すぐに結果が返ってきます。まったく同じ検索で何も見つからない場合は、一貫性があります。 –
これで分、日、またはfindit入力文字列に問題があるようですか? – Calimero