Php 需要扫描目录并仅删除特定类型的文件

Php 需要扫描目录并仅删除特定类型的文件,php,html,Php,Html,我需要有一个php脚本,允许我扫描一个目录,然后从中删除文件,我的代码工作得很好,它列出了我需要的所有内容,但我的问题是,我只需要从该目录中删除一个特定的文件,这个脚本会扫描所有内容,在这种情况下,我尝试只删除创建的.sql文件。但是,此脚本列出了所有类型的文件 我在下面留下我的代码: <?php $fid= $_POST['fid']; if (("submit")&&($fid != "")) { foreach($fid as $rfn) { $remove

我需要有一个php脚本,允许我扫描一个目录,然后从中删除文件,我的代码工作得很好,它列出了我需要的所有内容,但我的问题是,我只需要从该目录中删除一个特定的文件,这个脚本会扫描所有内容,在这种情况下,我尝试只删除创建的.sql文件。但是,此脚本列出了所有类型的文件

我在下面留下我的代码:

    <?php
$fid= $_POST['fid'];
if (("submit")&&($fid != "")) {
foreach($fid as $rfn) {
$remove = "$dire/$rfn";
unlink($remove);
}
}
$handle=opendir($dire);
while (($file = readdir($handle))!== false){
if ($file != "." && $file != "..") {
$size = filesize("$dire/$file");
$list .= '<table class="table table-bordered table-dark table-hover" cellspacing="0" width="100%">';
$list .= '<tbody>';
$list .= '<tr style="text-transform:uppercase;">';
$list .= '<td><small>'.$file.'</small></td>';
$list .= '<td align="center"><small><input type="checkbox" class="form-control" name="fid[]" value="'.$file.'"></small></td>';
$list .= '</tr>';
$list .= '</tbody>';
$list .= '</table>';
}
}
closedir($handle);
echo $list;
?>


正如您所看到的,这段代码运行得很好,我知道不太好,但我需要有一种方法来只显示我的SQL文件,而不显示其他类型的文件。

我不久前遇到了PHP的
递归DirectoryIterator
。这个内置类将允许您迭代一个目录及其子目录,并对其中的文件执行任何操作

请看下面的示例,了解如何实现这一点:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    // The directory to remove files from
    $directory = $_POST['directory'];

    // The extension se are looking for
    $extension = $_POST['extension'];

    // Construct the iterator
    $it = new RecursiveDirectoryIterator($directory);

    // Loop through files
    foreach(new RecursiveIteratorIterator($it) as $file) {
        if ($file->getExtension() == $extension) {
            echo 'Removing ' . $file . "\n";
            unlink($file);
        }
    }
}
实施 您可以用以下脚本替换代码。请注意,我在本例中使用了
directoryinterator
,因为您只希望遍历单个目录

<?php
/**
 * Directory overview
 */
// The directory to remove files from
$directory = '/path/to/directory';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    foreach ($_POST['fid'] as $filePath) {
        unlink $filePath;
    }
}
?>


<table class="table table-bordered table-dark table-hover" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>File</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <?php foreach (new DirectoryIterator($directory) as $file): ?>
        <?php if ($file->isDot() || !$file->isFile()) continue; ?>
}        <tr style="text-transform:uppercase;">
            <td><small><?= $file; ?></small></td>
            <td align="center"><small><input type="checkbox" class="form-control" name="fid[]" value="<?= $file->getPathName(); ?>"></small></td>
        </tr>
    </tbody>
</table>

文件
}        

不久前,我遇到了PHP的
RecursiveDirectoryIterator。这个内置类将允许您迭代一个目录及其子目录,并对其中的文件执行任何操作

请看下面的示例,了解如何实现这一点:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    // The directory to remove files from
    $directory = $_POST['directory'];

    // The extension se are looking for
    $extension = $_POST['extension'];

    // Construct the iterator
    $it = new RecursiveDirectoryIterator($directory);

    // Loop through files
    foreach(new RecursiveIteratorIterator($it) as $file) {
        if ($file->getExtension() == $extension) {
            echo 'Removing ' . $file . "\n";
            unlink($file);
        }
    }
}
实施 您可以用以下脚本替换代码。请注意,我在本例中使用了
directoryinterator
,因为您只希望遍历单个目录

<?php
/**
 * Directory overview
 */
// The directory to remove files from
$directory = '/path/to/directory';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    foreach ($_POST['fid'] as $filePath) {
        unlink $filePath;
    }
}
?>


<table class="table table-bordered table-dark table-hover" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>File</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <?php foreach (new DirectoryIterator($directory) as $file): ?>
        <?php if ($file->isDot() || !$file->isFile()) continue; ?>
}        <tr style="text-transform:uppercase;">
            <td><small><?= $file; ?></small></td>
            <td align="center"><small><input type="checkbox" class="form-control" name="fid[]" value="<?= $file->getPathName(); ?>"></small></td>
        </tr>
    </tbody>
</table>

文件
}        

如果只想在删除前检查文件扩展名:

<?php
    foreach ($fids as $fid) {
        if (pathinfo($dire . '/' . $fid)['extension'] == 'sql') {
            unlink($dire . '/' . $fid); 
        }
    }
?>

否则,您只能用GLOB()扫描目录中的.sql




如果只想在删除前检查文件扩展名:

<?php
    foreach ($fids as $fid) {
        if (pathinfo($dire . '/' . $fid)['extension'] == 'sql') {
            unlink($dire . '/' . $fid); 
        }
    }
?>

否则,您只能用GLOB()扫描目录中的.sql





只需检查文件扩展名,并使用该扩展名执行所需操作-
$extension=pathinfo(“$dire/$file”,pathinfo\u扩展名)是否考虑过使用GLOB()并只扫描sql文件$files=GLOB($dire./*{.sql}',GLOB_括号)@Autista_z是的,但是你能给我举个例子吗?@MichaelEugeneYuen说实话,我没有,因为我的代码正在工作,我所需要的是脚本识别sql文件。只需检查文件扩展名,然后用它做你想做的事情-
$extension=pathinfo($dire/$file),pathinfo_extension)是否考虑过使用GLOB()并只扫描sql文件$files=GLOB($dire./*{.sql}',GLOB_括号)@Autista_z是的,但是你能给我举个例子吗?@MichaelEugeneYuen说实话,我没有,因为我的代码正在工作,我所需要的是脚本识别sql文件。感谢你对这个PHP类脚本的帮助,我将尝试修改这个脚本,同时如果可以,有没有办法将它添加到我现有的脚本中?我该怎么做呢?@PedroCarvalho看看我答案中的
实现部分。嗨@Peter,它确实也很好用。您的实现非常好,感谢您的帮助。祝你一切顺利。感谢你对这个PHP类脚本的帮助,我将尝试修改这个脚本,同时如果可以,有没有办法将它添加到我现有的脚本中?我该怎么做呢?@PedroCarvalho看看我答案中的
实现部分。嗨@Peter,它确实也很好用。您的实现非常好,感谢您的帮助。祝你万事如意。那真是太棒了。非常感谢,不过我在表视图下对代码做了一个更改,将表放在后面,现在它非常完美,非常感谢您的帮助。所以我想您只想为每个表列出一个文件。这很好。很高兴我的脚本能帮上忙,这是正确的!非常感谢你,真的谢谢你!那击中了金牌。非常感谢,不过我在表视图下对代码做了一个更改,将表放在后面,现在它非常完美,非常感谢您的帮助。所以我想您只想为每个表列出一个文件。这很好。很高兴我的脚本能帮上忙,这是正确的!非常感谢你,真的谢谢你!