PHP 文件删除问题?

发布于 2024-11-14 17:32:04 字数 1726 浏览 2 评论 0原文

我有以下 HTML 表单:

<form action='delete.php' method='POST'>
    <table>
        <div class = '.table'>
            <?php
            $dir = '../uploads/store/';
            $newdir = ereg_replace('\.','',$dir);

            if (is_dir($dir)) {
               if ($dh = opendir($dir)) {
                  while (($file = readdir($dh)) !== false) {
                     if (!preg_match('/^(\.(htaccess|\.)?|index\.html)/',$file)) {
                        echo "<tr><td><font color = 'white'><a href='$newdir$file'>$file</a></font></td>";
                        echo "<td><input type='submit' value ='Delete'></td></tr>";
                        echo "<input align='right' type='hidden' value='$file' name='file'>";
                     }
                  }
                  closedir($dh);
               }
            }
            ?>
        </div>
    </table>
</form>

链接到以下 PHP 脚本:

<?php
    session_start();

    $file = $_POST['file'];
    $dir = '../uploads/store/';
    $file = $dir . $file;

    opendir($dir);

    if(unlink($file)) {
        echo "File sucessfully deleted";
        $_SESSION['username'] = 'guitarman0831';
        header('Refresh: 2;url=http://www.ohjustthatguy.com/uploads/uploads.html');
    } else {
        echo "Error: File could not be deleted";
        $_SESSION['username'] = 'guitarman0831';
        header('Refresh: 2;url=http://www.ohjustthatguy.com/uploads/uploads.html');
    }

?>

但是,当在 HTML 表单中按下“删除”按钮时,要删除的项目上方的项目将被删除。

希望这是有道理的。

注意:我不会为了这些脚本的安全性而考虑,我稍后会处理这个问题。目前只有我在使用这项服务。

I have the following HTML form:

<form action='delete.php' method='POST'>
    <table>
        <div class = '.table'>
            <?php
            $dir = '../uploads/store/';
            $newdir = ereg_replace('\.','',$dir);

            if (is_dir($dir)) {
               if ($dh = opendir($dir)) {
                  while (($file = readdir($dh)) !== false) {
                     if (!preg_match('/^(\.(htaccess|\.)?|index\.html)/',$file)) {
                        echo "<tr><td><font color = 'white'><a href='$newdir$file'>$file</a></font></td>";
                        echo "<td><input type='submit' value ='Delete'></td></tr>";
                        echo "<input align='right' type='hidden' value='$file' name='file'>";
                     }
                  }
                  closedir($dh);
               }
            }
            ?>
        </div>
    </table>
</form>

Which links to the following PHP script:

<?php
    session_start();

    $file = $_POST['file'];
    $dir = '../uploads/store/';
    $file = $dir . $file;

    opendir($dir);

    if(unlink($file)) {
        echo "File sucessfully deleted";
        $_SESSION['username'] = 'guitarman0831';
        header('Refresh: 2;url=http://www.ohjustthatguy.com/uploads/uploads.html');
    } else {
        echo "Error: File could not be deleted";
        $_SESSION['username'] = 'guitarman0831';
        header('Refresh: 2;url=http://www.ohjustthatguy.com/uploads/uploads.html');
    }

?>

However, when the Delete button is pressed in the HTML form, the item above the one intended to delete is deleted.

Hope this makes sense.

NOTE: I'm not going for security with these scripts, I'm going to work on that later. It's only me using this service right now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仄言 2024-11-21 17:32:04

您的 HTML 表单需要让各种提交按钮传递 $file 的值,而不是使用隐藏字段。

问题是,当您提交表单时,所有隐藏字段都会被 POST 到delete.php。然后,由于您尚未使用 PHP 友好的 HTML 数组变量语法,PHP 仅使用最后一个语法来设置 $_POST['file'] 的值。如果您在 delete.php 中执行 $_POSTvar_dump,您将看到 POSTed 输入是什么。

使用当前标记,最简单的事情就是将每个提交按钮命名为 file 并传递 $file 作为其 value。即你可以这样做:

<button name="file" value="example.txt" type="submit">Delete</button>

或者,你可以使用单选按钮或其他一些标记。

Your HTML form needs to have the various submit buttons pass the value for $file instead of using hidden fields.

The problem is that all of the hidden fields are POSTed to delete.php when you submit the form. Then, since you haven't used the PHP-friendly HTML array variable syntax, PHP uses only the last of these to set the value of $_POST['file']. If you do a var_dump of $_POST in delete.php, you will see what the POSTed input is.

The easiest thing to do, with your current markup, is just to have each submit button be named file and pass $file as its value. i.e. you could do:

<button name="file" value="example.txt" type="submit">Delete</button>

Alternately, you could use radio buttons or some other markup.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文