duanke8011
2016-09-29 19:50
浏览 55
已采纳

选择并显示服务器文件

I'm using shell_exec to execute a command on my server and show the result with echo.

Now, I want to select and display the files displayed. How can I do this?

<body>
    <form method="get" action="<?php echo $_SERVER["PHP_SELF"] ?>">
        <input type="submit" class="reset_button"/>
        <p><b>Search</b></p>
        <?php
            <input type='text' name='idtest' value='' />
            <input type='submit' value='consultar' />
        ?>
        <?php
            $consulta=$_GET[idtest];
            $salida =  shell_exec('find / -iname *'.$consulta.'*|sort');    
            echo "<pre>$salida</pre>";
        ?>
</body>

Result from code:

text1.txt
text2.txt
text3.txt
text4.txt

图片转代码服务由CSDN问答提供 功能建议

我正在使用 shell_exec 在我的服务器上执行命令并显示结果 回声。

现在,我想选择并显示显示的文件。 我该怎么做?

 &lt; body&gt; 
&lt; form method =“get”action =“&lt;?php echo $ _SERVER [”PHP_SELF“]?  &gt;“&gt; 
&lt; input type =”submit“class =”reset_button“/&gt; 
&lt; p&gt;&lt; b&gt;搜索&lt; / b&gt;&lt; / p&gt; 
&lt;?php \  n&lt; input type ='text'name ='idtest'value =''/&gt; 
&lt; input type ='submit'value ='consultar'/&gt; 
?&gt; 
&lt;?php  
 $ consulta = $ _ GET [idtest]; 
 $ salida = shell_exec('find / -iname *'。$ consulta。'* | sort');  
 echo“&lt; pre&gt; $ salida&lt; / pre&gt;”; 
?&gt; 
&lt; / body&gt; 
   
 
 

代码结果:

  text1.txt 
text2.txt 
text3.txt 
text4.txt 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dtt78245 2016-09-30 14:28
    已采纳

    $salida = shell_exec('find / -iname '.$consulta.' |sort');

    This isn't going to work the way you expect; the *'.$consulta.'* will be expanded to match anything in the current directory. It should be \'*'.$consulta.'*\'

    I'm also struggling to imagine where it would make sense to ever expose such functionality via a webserver - its going to be expensive to search the whole filesystem and potentially exposes a lot of stuff which should not be accessible remotely.

    How can I do this?

    If it were me....

     <?php
     $consulta=$_GET[idtest];
     $salida = explode("
    ", shell_exec('find / -iname \*'.$consulta.'\* | sort'));
     foreach ($salida as $s) {
        if (is_readable($s)) {
           print "<a href='filereader.php?src=" . urlencode($s)
             . "'>" . htmlentities($s) . "</a><br />";
        } else {
           print htmlentities($s) . "<br />";
     }
    ?>
    

    Content of filereader.php should be obvious.

    打赏 评论
  • dongqiaochi2711 2016-09-30 13:27

    Here is how to display everything:

    You could add this snippet below the last one, above </body>:

    <?php
       $consulta=$_GET[idtest];
       $alltext = shell_exec('find / -iname *'.$consulta.'* |sort|xargs -I{} cat {}');    
    echo "<pre>$alltext</pre>";
    ?>
    

    Now what you want is a little trickier: just read $salida as an array, thusly creating a link per file to a new_page.php;

    Then on the new_page.php:

    <?php
       $filename=$_GET[filename];
       $onetext = shell_exec('cat '.$filename);    
       echo "<pre>$onetext</pre>";
    ?>
    
    打赏 评论

相关推荐 更多相似问题