嗨,我在mongodb中存储了一些文件名,我在本地目录中存储了一些文件,现在我的要求是提取与db中的值匹配的文件的本地路径,它不应该匹配文件中的特定字符串,它应该与完整的字符串匹配。你能告诉我怎么做吗?
示例:sample-php-book.pdf
是db值,它应该与sample-php-book.pdf
文件名匹配,而不是与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
的值谢谢
如果我理解正确的话,你是在寻找这样的东西:
编辑:我不知何故忘记了拆分使用regex而不是简单的搜索。因此我将split替换为explosion
<?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
}
}
?>