Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php删除文件的按钮_Php_Loops_Delete File - Fatal编程技术网

使用php删除文件的按钮

使用php删除文件的按钮,php,loops,delete-file,Php,Loops,Delete File,我有一个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_han

我有一个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取消链接,但到目前为止,我只能在任何人访问脚本时删除所有文件,这不是我想要的


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

尝试,注意:您必须修复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);

试试看,注意:您必须修复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);

创建另一个脚本并通过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>";

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

创建另一个脚本并通过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>";

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

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

您还忘记了列表中的
标记。查看W3C标准中的列表

一个简单的实现是基于带有提交按钮的元素。这是纯html。在表单中插入
  • 元素,每个元素都有一个“删除”按钮。指定窗体的目标

    生成此(或类似)html:

    如果像这样循环使用$\u 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);
        }
    }
    

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

    您还忘记了列表中的
    标记。查看W3C标准中的列表

    一个简单的实现是基于带有提交按钮的元素。这是纯html。在表单中插入
  • 元素,每个元素都有一个“删除”按钮。指定窗体的目标

    生成此(或类似)html:

    如果像这样循环使用$\u 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);
        }
    }
    

    是的,考虑一下安全性!code>$\u GET['f']=bla/./../bla.bla
  • ^应在POST中发送文件名。将有1以上的。@ USE1032531,我增加了替换…..,谢谢你的修正。是的,考虑一些安全性!code>$\u GET['f']=bla/./../bla.bla
    ^应在POST中发送文件名。如果是+1,@user1032531,我为../添加了替换,谢谢您的更正。