为上传的文件添加删除按钮(取消链接)
我制作了文件上传表单,以便登录的用户可以将文件上传到自定义文件夹(uploads/users/username /)。为上传的文件添加删除按钮(取消链接)
下面的代码显示了用户上传的所有文件,但是,我一直在努力为每个项目添加一个“删除”按钮。这是一个严格控制的网站,没有注册,并且不适用于公共领域,所以安全性不是一个巨大的问题,因为这只会被最多15-20个用户使用。
我一直在努力整齐地添加在取消链接。我试图找到各种建议在线,但没有运气,我认为它应该是这个样子:
<?php
$dir_open = opendir('uploads/users/' . $_SESSION['username'] . '');
while(false !== ($filename = readdir($dir_open))){
if($filename != "." && $filename != ".."){
$fullpath = "uploads/users/" . $_SESSION['username'] . "/$filename";
echo "<a href='$fullpath' target=\"_blank\"><img src='$fullpath' style='width:350px;border: 1px solid #000;'></a><br />
$filename<br />
Delete button--> <a href="$fullpath">Delete file</a>
<hr><br />";
}}closedir($dir_open);
?>
任何帮助,将不胜感激
要为每个文件创建一个删除按钮,您需要获取文件的id和路径,假设您在创建上传功能时将该信息存储在数据库中。因此,例如可能看起来像:
<?php
//...
$db = new PDO('mysql:host=127.0.0.1;dbname=db_name','db_user','db_pass');
$stmt = $db->prepare('SELECT id,filename FROM your_table');
while ($row = $stmt->fetch()){
?>
<a href="delete.php?id=<?php echo $row['id']; ?>">Delete</a>
<?php
}
?>
然后,在delete.php
文件:
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
//db connect here
$stmt = $db->prepare('DELETE FROM table_name WHERE id=?');
$stmt->execute([$_GET['id']);
}
在安全方面,我不能告诉你很多有关,因为我不知道你想要什么。但是您可以检查MIME类型(并只允许您想要的MIME类型)以及文件大小(仅允许某些文件大小,或禁止0大小的文件)。只是搜索谷歌,有很多关于这个问题的好教程。
并列出所有上传的文件,对于每个用户,您也可以使用db。
LE
好吧,我挖你的问题一点点,我终于找到了一种方法,让你的用户删除文件。但请注意,我没有考虑到任何安全性。这是你的责任,即使你也有这个问题。
因此,代码如下所示:
//index.php
$files = scandir('files');
foreach ($files as $file){
if (is_file('files/'.$file)){
?>
<a href="index.php?remove=<?php echo $file; ?>">delete</a> <br>
<?php
}
}
if(isset($_GET['remove'])){
unlink('files/'.$_GET['remove']) ? header('Location: index.php') : print 'Not Removed<br>';
}
我试图代码之前使用的chmod 777 -R files/
。我真的不知道这是否是一个好方法。
LE2
我用这个例子的目录结构:
project/
files/
1.php
2.html
3.css
4.js
index.php
这些文件(1.php
,2.html
等)通过你的上传功能都摆在那里,你说你已经拥有了。 index.php
首先将files/
目录作为数组读取(仅包含文件名);然后遍历数组,并检查数组中的每个元素是否为文件,如果是,则会生成一个链接,并将用户重定向回index.php
,但这次有一个查询字符串(例如:index.php?remove=1.php
)。然后,最后一个检查问题是否有一个名为remove
的查询字符串,如果有,它将删除相应的文件并将重定向回index.php
。
I think what you need is make sure that when saving the file, you create a unique name to save the file by changing the file name to something of such
<?php
$fileExtension = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION);
$filename=uniqid('upload_').rand(10000,9999999).date('ymdHis').rand(10000,9999999).'.'.$fileExtension;
?>
use this as the file name (containing the name and the extension) you are saving to ur db and move the file to a folder.
To delete, the delete.php will pass the filename as a get method or using ajax to send the file name to your delete.php
such that in delete.php, you will then
`1. query to delete the file using filename from db
2. if success, then use the unlink to delete from path such that you do something of this nature
<?php
unlink(Path_of_uploaded_files/filename);
?>
I can provide an example later.
嗨Dan,我已经在'$ fullpath'中有完整的文件路径。我没有使用数据库的文件,因此我相信'unlink()'是这样的方式。我有一个类似的设置,我在家里使用我的内联网,并使用预建的系统与数据库来处理所有登录/文件和工作正常,但是,在这种情况下不适合作为我正在工作的网站限于一个分贝,并已被使用。我知道这可以在没有数据库的情况下完成,但我已经阅读了谷歌前5页的每一个结果,并且这些情况都没有帮助。 – noozl
@noozl看看我的LE。 –
我仍然无法用我当前的代码实现。我对PHP并不擅长,所以我正在为此付出一点努力。任何指针在哪里放置什么? – noozl