drzyeetvt41077335 2013-12-19 04:34
浏览 69
已采纳

如何在文件托管站点上使用PHP删除目录中的文件?

I am trying to make a simple file hosting site for my family, a couple friends and I to use. I have the entire system setup except one thing... I cannot find a way to let the user delete a file without having to access the FTP. I will post the code in which I use to list the files to the user below. I want to have a delete button automatically generated for each new file the user uploads.

Code to list files:

$directory = 'uploads/' . $_SESSION['user'] . '/';
if ($handle = opendir($directory)) {
    echo '<h3>Your files are listed below</h3>';    
    while ($file = readdir($handle)) {
        if ($file != '.' && $file != '..') {
            echo '<a target="_blank" href="' . $directory . '/' . $file . '">' . $file . '<br>';    
        }
    }
}
  • 写回答

2条回答 默认 最新

  • doupingmao1903 2013-12-19 04:53
    关注
    $directory = 'uploads/' . $_SESSION['user'] . '/';
    
    if(isset($_REQUEST['DelFile'])) {
        $DeleteFile = $_REQUEST['DelFile'];
        if(file_exists($directory.$DeleteFile)) {
            @unlink($directory.$DeleteFile);
            header("location:SamePageURL.php?msg=1");
        } else header("location:SamePageURL.php?msg=2");
    }
    
    if ($handle = opendir($directory)) {
    echo '<h3>Your files are listed below</h3>';    
        while ($file = readdir($handle)) {
            if ($file != '.' && $file != '..') {
                echo '<a target="_blank" href="'.$directory.'/'.$file.'">' . $file.' <a href="SamePageURL.php?DelFile='.$file.'">Delete</a> <br>';  
            }
        }
    }
    
    if(isset($_REQUEST['msg'])) {
        $Message = $_REQUEST['msg'];
        if($Message == 1) echo "File deleted sucessfully";
        else if($Message == 1) echo "File not found";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料