php - 从数组中取消链接()

标签 php mysql arrays

不太了解如何在数组中的每个项目上运行函数,对 PHP 来说还是很陌生,下面的 $pic_loc 是来自 MySQL 的数据数组,其中包含与任何内容匹配的图像的路径和文件名搜索条件已执行。

尝试让 unlink 函数在它找到的每个人上运行。没有错误返回。我认为 foreach 不是正确的方法,有人能指出我正确的方向吗?

 $pic_loc = $cell_pictures['pic_loc']; //gathers an array from the database of files locations

    $pics_to_delete .= "../../".$pic_loc; //corrects the directory

    foreach($pics_to_delete as $pics_being_deleted) //deletes all pictures found
    {

    unlink($pics_being_deleted);

    }

编辑: 我对这两个答案进行了很多修改,我知道我可以删除两层文件,因为我可以毫无问题地对单个文件执行此操作。这就是我目前正在做的事情,我摆脱了 while() 循环,认为这可能会导致 foreach 运行出现问题:

$data_array2 = mysql_query("SELECT * FROM pictures WHERE user_id = '".$id."'");
$cell_pictures = mysql_fetch_array($data_array2);


foreach($cell_pictures['pic_loc'] as $pics_being_deleted) //deletes all pictures found
{

unlink("../../".$pics_being_deleted);

}

这是它现在返回的错误:

Warning: Invalid argument supplied for foreach() // on the line that the foreach is on

编辑: 好吧,这很奇怪,但我明白了!您可能会认为来自查询的数据是一个数组,但实际上您必须将其设置为数组。我就是这样做的:

$data_array2 = mysql_query("SELECT pic_loc FROM pictures WHERE user_id = '".$id."'");


while($cell_pictures = mysql_fetch_array($data_array2))
{
$the_pics = $cell_pictures['pic_loc'];
$pics_as_array = array($the_pics);
}

foreach($pics_as_array as $pics_being_deleted) 
{

unlink("../../".$pics_being_deleted);


}

这效果非常好。非常感谢您花时间阅读本文! -迈克

最佳答案

实际上 foreach 完全适合你想做的事情。

但是,您需要将一些逻辑放入迭代中。让我们看看:

$directory = realpath("../../"); // the directory where files are placed
foreach ($pics_to_delete as $basename) //deletes all pictures found
{
    $path = sprtinf('%s/%s', $directory, $basename);
    unlink($path);
}

更好的选择是反过来做。首先获取目录及其中的所有文件,然后根据要删除的文件过滤该列表。仅当名称匹配时,才继续:

$dir = new  DirectoryIterator($path);
$toDelete = new DeleteFilter($dir, $pics_to_delete);
foreach ($toDelete as $file)
{
    printf("Delete: %s\n", $file->getPathname());
    unlink($file->getPathname());
}

DirectoryIterator 随 PHP 一起提供,DeleteFilter 是一个小型 FilterIterator,可确保仅从 $ 匹配和输入真实文件将返回 pics_to_delete 数组:

class DeleteFilter extends FilterIterator
{
    private $filenames;

    public function __construct(Iterator $iterator, array $filenames)
    {
        $this->filenames = $filenames;
        parent::__construct($iterator);
    }

    public function accept()
    {
        $current = $this->getInnerIterator()->current();
        if (!($current instanceof SplFileInfo)) {
            return FALSE;
        }
        if (!$current->isFile()) {
            return FALSE;
        }
        return in_array($current->getBasename(), $this->filenames);
    }
}

您不需要那个过滤器迭代器,而是可以在 foreach 中编写该逻辑,但我认为这个更流畅。

关于php - 从数组中取消链接(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10172083/

相关文章:

php - 在 AJAX 中调用 PHP 脚本时出现错误消息 : Object not found!

PHP json_decode - 处理命名空间

php - 无效对象或资源 mysqli_stmt 错误

mysql - 有没有更好的方法在 MySQL 中编写 NOT IN(子查询)?

c# - 如何防止两个线程复制方法和变量

C++ 和数组递归

php - 在 Alpine 上运行 nginx

javascript - 我应该在 node.js 中对 "gzuncompress()"使用什么编码?

mysql - 如何编写sql代码来获取2个日期之间的数据?

c# - 将 1d 数组索引转换为 3d 数组索引?