I have a PHP script where you can upload files. These files get listed, and are converted to a download link. The last thing that I need is a delete button for each list item. Like so
我有一个PHP脚本,您可以上传文件。这些文件将被列出,并转换为下载链接。我需要的最后一件事是每个列表项的删除按钮。像这样
- test.txt X
(The large X should be a delete button).
(大X应该是删除按钮)。
This is my code so far.
到目前为止这是我的代码。
<?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>
Im quite new with PHP, so I that hope someone can explain me how it works in easy language.
我是PHP的新手,所以我希望有人能用简单的语言解释它是如何工作的。
3 个解决方案
#1
1
The last thing what I need is a delete button for each list item
我需要的最后一件事是每个列表项的删除按钮
Post the value via a form:
通过表格发布价值:
<form method="post" action="delete.php">
<button type="submit" name="file_id" value="some_value">×</button>
</form>
And then in delete.php
, reference $_POST['file_id']
.
然后在delete.php中,引用$ _POST ['file_id']。
Another method is to wrap your while
loop in the form:
另一种方法是在表单中包装while循环:
<form method="post" action="delete.php">
<?php while(...): ?>
<button type="submit" name="file_id" value="some_value">×</button>
<?php endwhile; ?>
</form>
#2
0
You may add the X link for referencing the file to be deleted i.e.
您可以添加X链接以引用要删除的文件,即
$thelist .= '<li><a download="'.$file.'"href="uploads/'.$file.'">'.$file.' </a> <a href="delete.php?item='.$file.'"> X </a></li>';
Then on your delete page you may add the unlink code to delete your file
然后在删除页面上添加取消链接代码以删除文件
$file = $_GET['item'];
if (!unlink($file))
{
echo ("Error deleting $file");
}
else
{
echo ("Deleted $file");
}
This is the basic idea of how to go about it. But to be safe you may encrypt the url file name on the parameter i.e.
这是如何实现它的基本思路。但为了安全起见,您可以加密参数上的url文件名,即
base64_encode($file)
And on the delete page you decode it
在删除页面上解码它
base64_decode($_GET["item"])
#3
0
In order to create a request for deleting the item, you can for instance:
为了创建删除项目的请求,您可以例如:
<form action="delete.php" method="POST">
<input type="hidden" name="csrf_token" value="$csrf_token">
<input type="hidden" name="file" value="$file">
<button type="submit">X</button>
</form>
Basically $csrf_token
is a secure random token which is tied to the session, which the server should validate before processing the delete request. (Updated regarding CSRF security concerns @PeeHaa mentioned.) (You can use method DELETE if you support it.)
基本上$ csrf_token是一个安全随机令牌,它与会话相关联,服务器在处理删除请求之前应该验证该会话。 (有关CSRF安全问题的更新@PeeHaa提到。)(如果您支持,可以使用方法DELETE。)
#1
1
The last thing what I need is a delete button for each list item
我需要的最后一件事是每个列表项的删除按钮
Post the value via a form:
通过表格发布价值:
<form method="post" action="delete.php">
<button type="submit" name="file_id" value="some_value">×</button>
</form>
And then in delete.php
, reference $_POST['file_id']
.
然后在delete.php中,引用$ _POST ['file_id']。
Another method is to wrap your while
loop in the form:
另一种方法是在表单中包装while循环:
<form method="post" action="delete.php">
<?php while(...): ?>
<button type="submit" name="file_id" value="some_value">×</button>
<?php endwhile; ?>
</form>
#2
0
You may add the X link for referencing the file to be deleted i.e.
您可以添加X链接以引用要删除的文件,即
$thelist .= '<li><a download="'.$file.'"href="uploads/'.$file.'">'.$file.' </a> <a href="delete.php?item='.$file.'"> X </a></li>';
Then on your delete page you may add the unlink code to delete your file
然后在删除页面上添加取消链接代码以删除文件
$file = $_GET['item'];
if (!unlink($file))
{
echo ("Error deleting $file");
}
else
{
echo ("Deleted $file");
}
This is the basic idea of how to go about it. But to be safe you may encrypt the url file name on the parameter i.e.
这是如何实现它的基本思路。但为了安全起见,您可以加密参数上的url文件名,即
base64_encode($file)
And on the delete page you decode it
在删除页面上解码它
base64_decode($_GET["item"])
#3
0
In order to create a request for deleting the item, you can for instance:
为了创建删除项目的请求,您可以例如:
<form action="delete.php" method="POST">
<input type="hidden" name="csrf_token" value="$csrf_token">
<input type="hidden" name="file" value="$file">
<button type="submit">X</button>
</form>
Basically $csrf_token
is a secure random token which is tied to the session, which the server should validate before processing the delete request. (Updated regarding CSRF security concerns @PeeHaa mentioned.) (You can use method DELETE if you support it.)
基本上$ csrf_token是一个安全随机令牌,它与会话相关联,服务器在处理删除请求之前应该验证该会话。 (有关CSRF安全问题的更新@PeeHaa提到。)(如果您支持,可以使用方法DELETE。)