dtzjvj3915 2015-11-04 19:03
浏览 20

删除php中的特定文件

I am practicing uploading and deleting files in php and I've encountered a problem.

I have this code:

if($files)
{
    foreach($files as $file)
    {
        echo '<a href="files_storage/'.$file.'" download>'.$file.'</a><br>';
    }
}
else
{
    echo 'There are currently no files.';
}

It displays all files from a directory. I want to add a delete button for every file. Something like this:

enter image description here

Should I create different form for every file? If I do so, how would I identify which file to be deleted when a form is submitted? Any suggestions?

  • 写回答

1条回答 默认 最新

  • drjk87189 2015-11-04 19:16
    关注

    You could put all the submit buttons in one form and set the name attribute of every button to delete[' . $file . ']. Then, in the beginning of the loop, check if $_POST['delete'][$file] is set and delete it.

    if(!empty($files)){
        echo '<form action="" method="post">';
        foreach($files as $file){
            if(isset($_POST['delete'][$file]) && file_exists($file)){
                unlink($file);
                continue;
            }
            echo '<a href="files_storage/'.$file.'" download>'.$file.'</a><input type="submit" name="delete[' . $file . ']" value="Delete this file" /><br>';
        }
        echo '</form>';
    }
    if(empty($files)){ //check again, since $files may be empty after deletion
        echo 'There are currently no files.';
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?