【问题标题】:Exec output sometimes incorrectly returns nothing?执行输出有时会错误地返回任何内容?
【发布时间】:2017-10-05 07:16:34
【问题描述】:

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

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

这适用于大多数搜索,但有时进行文本搜索,它不会返回任何内容。我已经对此进行了调试,以查看传递给“查找”的内容并且始终有效,因此我调用 exec 和解析输出的方式一定有问题。但我不知道它是什么。我在 php.net 中看到了一些关于使用不同方法执行 shell 命令并将输出返回给 PHP 程序的讨论,但我并没有成功地让它在所有情况下都始终工作。

我认为这可能是 Linux 文件权限问题,但它在文本搜索中找到的文件与未找到的文件具有相同的权限、所有权和组。同样,我知道这些文件在那里,因为如果我自己登录到 shell 并使用 find 进行搜索,我可以找到它们。我应该提一下,没有 shell 登录帐户,所以我无法“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>

【问题讨论】:

  • 我在您的 php 代码中看不到任何明显的问题,可能只是 exec 调用需要很长时间才能完全运行吗?你检查过max_execution_time php 设置了吗?当搜索中断时,生成的 html 输出是否不完整?
  • @Calimero 感谢您查看代码。不,它会立即返回,要么有结果要么什么都没有。它是一致的,如果它什么也没找到,它总是在完全相同的搜索中找不到任何东西。
  • 好的,那么问题似乎涉及分钟、天或 findit 输入字符串?
  • 刚刚找到!我查看了 Samba 共享为目录设置的权限。我只是在看文件。它们在 smb.conf 中设置为目录模式 = 0770,目录应该是 0775。

标签: php linux shell


【解决方案1】:

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

directory mode = 0770

什么时候应该是 0775 以便用户 apache 可以找到它。这个 samba 共享有自己的帐户。虽然这看起来安全性很低,但实际上此文件服务器位于 LAN 上并位于防火墙后面。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多