【问题标题】:Button to delete a file using php使用php删除文件的按钮
【发布时间】:2015-07-04 04:29:19
【问题描述】:

我有一个 php 循环,它显示目录中的所有文件:

<?php

    // Define the full path to your folder from root
    $filepath =  'castlewp/cv/'; // The place the files will be uploaded to.
    $path = $_SERVER['DOCUMENT_ROOT'] . $filepath;

    // Open the folder
    $dir_handle = @opendir($path) or die("Unable to open $path");

    // Loop through the files
    while ($file = readdir($dir_handle)) {

    if($file == "." || $file == ".." || $file == "index.php" || $file == ".htaccess" || $file == ".htpasswd" )

        continue;
        echo "<li><a href=\"$file\">$file</a></li>";

    }
    // Close
    closedir($dir_handle);
?>

我正在尝试找到一种在每个文件旁边添加一个按钮的方法,该按钮将删除该文件。我一直在玩 php unlink 但到目前为止我只能在有人访问脚本时删除所有文件,这不是我想要的。

据我所知,这是一个相当简单的添加内容,但我对 PHP 的了解很少,所以任何建议都将不胜感激。

【问题讨论】:

    标签: php loops delete-file


    【解决方案1】:

    创建另一个脚本并通过 GET 参数将文件名传递给它,并在当前文件中添加如下内容:

    if($file == "." || $file == ".." || $file == "index.php" || $file == ".htaccess" || $file == ".htpasswd" )
    
        continue;
        echo "<li><a href=\"$file\">$file</a></li>";
        echo "<a href=\"delete.php?f={$file}\">Delete</a>";
    

    然后在你的 delete.php 文件中复制一些你现有的代码并修改它以删除文件:

    $filepath =  'castlewp/cv/'; // The place the files will be uploaded to.
    $path = $_SERVER['DOCUMENT_ROOT'] . $filepath;
    if(empty($_GET['f']))
      exit();
    $file = str_replace(array('/', '..'), '', $_GET['f']);
    $filePath = realpath($path.$file);
    if($filePath !== FALSE)
      unlink($filePath);
    

    以这一切为例。您仍然需要添加一些安全性,但我认为这是一个起点。

    【讨论】:

    • 是的,考虑一些安全性! $_GET['f']=bla/../../bla.bla
    • ^ 应该在 POST 中发送文件名。会为此 +1。
    • @user1032531,我添加了替换../,感谢您的更正。
    【解决方案2】:

    尝试,注意:您必须修复 li 标签中的 url 以匹配您的 url

    <?php
    // Define the full path to your folder from root
    $filepath =  'castlewp/cv/'; // The place the files will be uploaded to.
    $path = $_SERVER['DOCUMENT_ROOT'] . $filepath;
    
    // check if a file is send via GET variable
    if (isset($_GET['file'])) {
    $fullpath = $path.$file;
    // check if file exists
    if(file_exists($fullpath)) {
        unlink($fullpath);  
    } else {
        // add some error handling here
    } 
    }
    
    // Open the folder
    $dir_handle = @opendir($path) or die("Unable to open $path");
    
    // Loop through the files
    while ($file = readdir($dir_handle)) {
    
    if($file == "." || $file == ".." || $file == "index.php" || $file == ".htaccess" || $file == ".htpasswd" )
    
        continue;
        echo "<li><a href=\"yoururl?file=$file\">$file</a></li>";
    
    }
    // Close
    closedir($dir_handle);
    

    ?>

    【讨论】:

      【解决方案3】:

      首先:正确格式化您的代码。我建议您使用空格和/或额外的括号来使您的代码更具可读性。 continue 语句是满足 if 条件时唯一执行的语句,但您的源格式不这样认为。

      您还忘记了列表的&lt;ul&gt;&lt;ol&gt; 标签。检查 W3C 标准以获取列表。

      一个简单的实现是基于一个带有提交按钮的元素。这是纯html。插入你的

    • 表单内的元素,每个元素都有一个“删除”按钮。指定表单的目标。

      生成这个(或类似的)html:

      <form action="myfilemanager.php" method="post" name"=formfilemanager">
          <ul>
            <li>filename1.dat <input type="submit" name="filename1.dat" value="delete"/></li>
            <li>filename2.dat <input type="submit" name="filename2.dat" value="delete"/></li>
            ....
          </ul>
      </form>
      

      当您按下 filename2.dat 的“删除”按钮时,您会在 $_POST global 上找到一些参数:

      filename2.dat=delete
      

      如果你像这样循环 $_POST 数组

      foreach($_POST as $k=>$v) {
          if($v=='delete') { 
            // $k contains the file name to delete
            // SANITIZE you input, remove '..','%', whatever
            $k=str_replace(array('%','|','/','..'),'',$k);
            unlink($filepath.$k);
          }
      }
      
    • 【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-20
        • 1970-01-01
        • 1970-01-01
        • 2013-06-28
        • 1970-01-01
        • 2012-12-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多