PHP,用于在浏览器中读取文件的脚本

im trying to make a script that will allow me to read the specific file im choosing from a form i made on html, this is my code:

<table border="2px">
    <tr>
        <td>
            <form method="post">
                <input type="submit" value="libros" name="libros">
                <input type="submit" value="imagenes">
                <input type="submit" value="musica" name="musica">
                <input type="submit" value="prueba" name="test">
            </form>
        </td>
    </tr>
</table>

<?php
if(isset($_POST['libros']))
    echo "exists!";

if(isset($_POST['musica']))
    echo "musica exists!";

if(isset($_POST['test']))
{
    $directory = 'C:\Users\Oscar\Documents\test';
    $files = array_diff(scandir($directory), array('..', '.'));
    $archive = array();

    foreach($files as $file):?>

    <table border="2px">
        <tr>
            <td>
                <form method="post">
                    <input type="submit" value="<?php echo $file; ?>" name="<?php echo $file; ?>">  
                </form>
            </td>
        </tr>
    </table>

    <?php endforeach;

    if(isset($_POST[$file]))
        echo 'showing '.$file;
?>

im making the tests on the test input, which will show everything that's in the folder, for now it just have txt files(1.txt, 2.txt, 3.txt), after clicking on test, its showing me all the file names with buttons on each one, but after i click on one of those buttons made for each file, it does nothing. Im trying to make me an echo at least but no clue.

foreach($files as $file){?>

<table border="2px">
    <tr>
        <td>
            <form method="post">
                <input type="submit" value="<?php echo $file; ?>" name="<?php echo $file; ?>">  
            </form>
        </td>
    </tr>
</table>

<?php }

try rewriting it with brackets after the foreach loop

Why should it show anything? You're explicitly testing for the presence of $_POST['test'] before running any of that code. But within the form that actually triggers the display of that file, you don't have ANY fields with the name test. You need something like:

<form method="post">
    <input type="submit" value="<?php echo $file; ?>" name="<?php echo $file; ?>">  
    <input type="hidden" name="test" value="test" />
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---- send a "test" field
</form>