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
$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.
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>";
?>