【问题标题】:PHP, script for reading files in browserPHP,用于在浏览器中读取文件的脚本
【发布时间】:2014-01-19 03:13:20
【问题描述】:

我正在尝试制作一个脚本,让我可以从我在 html 上制作的表单中读取我选择的特定文件,这是我的代码:

<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;
?>

我在测试输入上进行测试,它将显示文件夹中的所有内容,现在它只有 txt 文件(1.txt、2.txt、3.txt),点击测试后,它显示给我所有文件名都带有按钮,但是在我单击为每个文件制作的按钮之一后,它什么也不做。我试图让我至少成为一个回声,但没有任何线索。

【问题讨论】:

  • 我猜不是,但我不知道为什么

标签: php


【解决方案1】:

为什么要显示任何内容?在运行任何代码之前,您正在明确测试 $_POST['test'] 的存在。但在实际触发显示该文件的表单中,您没有任何名称为test 的字段。你需要这样的东西:

<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>

【讨论】:

  • 其实Marc,之前有人发过一个答案,因为OP一直在问问题,所以把它删除了。只是一个快速的FYI ;-) 我设法让它有点工作,但我不会费心让它成为一个答案。我不会掉进那个“蠕虫罐头”。
【解决方案2】:
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 }

尝试在 foreach 循环之后用括号重写它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 2011-04-19
    相关资源
    最近更新 更多