Exec输出有时会错误地返回什么?

时间:2017-10-05 07:16:34

标签: php linux shell

我在局域网上有一个Linux文件服务器,希望能够让那些不知道如何使用shell的用户搜索文件服务器上的文件。

我决定编写一个PHP程序,以便可以在Web浏览器中使用,用户可以在其中输入搜索字符串,并快速列出匹配项。它使用PHP exec函数,并使用Linux find命令。

这适用于大多数搜索,但有时进行文本搜索,它什么都不返回。我调试了这个以查看传递给'发现'这总是有效的,所以我调用exec和解析输出的方式肯定有问题。但我不知道它是什么。我在php.net中看到了一些关于使用不同方法执行shell命令并将输出返回给PHP程序的讨论,但是我并没有成功地让它在所有情况下都能正常工作。

我认为它可能是Linux文件权限问题,但它在文本搜索中找到的文件具有与它找不到的权限,所有权和组相同的权限。同样,我知道那些文件存在,因为如果我自己登录shell并使用find进行搜索,我可以找到它们。我应该提一下,没有shell登录帐户,所以我无法提供su' su'到该帐户并在shell中测试我的find命令作为共享用户。

这是Web 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>&nbsp;</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>&nbsp;</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>

1 个答案:

答案 0 :(得分:1)

我忽略了Samba将目录的权限设置为

directory mode = 0770

当它应该是0775时,用户apache可以找到它。这个samba共享有自己的帐户。虽然这可能看起来安全性较低,但是这个文件服务器位于局域网上并位于防火墙后面。

这解释了为什么PHP中的exec命令无法返回$ output,因为它作为用户apache而不是Samba共享用户运行,后者需要这些目录拥有0775的权限。