0

嗨,我已经在 mongodb 中存储了一些文件名,并且我在本地目录中存储了一些文件,现在我的要求是提取与 db 中的值完全匹配的文件的本地路径,它不应该与文件中的特定字符串匹配它应该与 complate 字符串匹配。你能建议我怎么做吗?

示例:sample-php-book.pdf它应该与sample-php-book.pdf文件名匹配的 db 值是否与sample.pdf

我使用了以下代码

<?php
$results = array();
$directory = $_SERVER['DOCUMENT_ROOT'].'/some/path/to/files/';
$handler = opendir($directory);

while ($file = readdir($handler)) {

        if(preg_match('$doc['filename']', $file)) {

            $results[] = $file;
        }
    }
}
?>

$doc[filename] 是来自 db 的值

谢谢

4

1 回答 1

0

如果我对您的理解正确,那么您正在寻找这样的东西:

编辑:我不知何故忘记了拆分使用正则表达式而不是简单的搜索。因此我用explode替换了split

<?php

// DB-Code here[..]

$arrayWithYourDbStrings; // <-- should conatain all strings you obtained from the db

$filesInDir = scandir('files/');
foreach($filesInDir as $file)
{
    // split at slash
    // $file = split('/', $file); <-- editted
    $file = explode('/', $file);

    // get filename without path
    $file = last($file);

    // check if filename is in array
    if(in_array($file, $arrayWithYourDbStrings))
    {
        // code for match
    }
    else
    {
        // code for no match
    }
}
?>
于 2013-05-29T11:30:35.800 回答