如何在PHP中创建取消链接按钮

我有一个
PHP脚本,您可以上传文件.这些文件将被列出,并转换为下载链接.我需要的最后一件事是每个列表项的删除按钮.像这样

> test.txt X.

(大X应该是删除按钮).

到目前为止这是我的代码.

<?php

  if(isset($_FILES['file_array'])){
    $name_array = $_FILES['file_array']['name'];
    $tmp_name_array = $_FILES['file_array']['tmp_name'];
    $type_array = $_FILES['file_array']['type'];
    $size_array = $_FILES['file_array']['size'];
    $error_array = $_FILES['file_array']['error'];
    for($i = 0; $i < count($tmp_name_array); $i++){
        if(move_uploaded_file($tmp_name_array[$i], "uploads/".$name_array[$i])){
        } else {
            echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
        }
    }
  }


   $thelist = "";
   if ($handle = opendir('uploads')) {
       while (false !== ($file = readdir($handle))) {
           if ($file != "." && $file != "..") {
              $thelist .= '<li><a download="'.$file.'"href="uploads/'.$file.'">'.$file.'</a></li>';
           }
      }
    closedir($handle);
  }
?>

<h1>List:</h1>
<ul><?php echo $thelist; ?></ul>

我是PHP的新手,所以我希望有人可以用简单的语言解释它是如何工作的.

最佳答案

The last thing what I need is a delete button for each list item

通过表格发布价值:

<form method="post" action="delete.php">
  <button type="submit" name="file_id" value="some_value">&times;</button>
</form>

然后在delete.php中,引用$_POST [‘file_id’].

另一种方法是在表单中包装while循环:

<form method="post" action="delete.php">
  <?php while(...): ?>
    <button type="submit" name="file_id" value="some_value">&times;</button>
  <?php endwhile; ?>
</form>
点赞