php - Exec 输出有时不正确地返回任何内容?

标签 php linux shell

我在局域网上有一台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>

最佳答案

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

directory mode = 0770

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

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

关于php - Exec 输出有时不正确地返回任何内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46579958/

相关文章:

linux - Wireshark 与 tcpdump/libpcap 中数据包的不同顺序?

linux - 如何使用sudo获取运行命令的pid

linux - Shell 脚本 - 从用户输入 4 行

linux - 如何检查 pod 中是否存在 ping 实用程序

javascript - 同一个 ActiveForm yii2 上的多个模型

java - 每当我运行 "apt-get install php5-gd php-mysql"时,它都会在 Ubuntu 14.04 上显示 "Unable to locate package php-mysql"

php - Laravel 内连接查询

python - 寻找可靠的 python 进程同步技术(Linux 不可移植)

linux - Eclipse 在 Linux 上崩溃

php - 如何根据用户点击 Jquery Galleria 幻灯片在开始时设置自定义图像