使用php检查文件的存在从名称“sample”开始

时间:2012-11-21 10:45:55

标签: php

有人可以帮助我如何使用PHP检查特定文件夹中以sample1.pdfsample12.pdf之类的任何名称开头的文件是否存在?下面是我检查单个文件的代码。

<html>
<?php
if(file_exists("properties/".$owner."/".$property."/sample1.pdf") )
?>
</html>

1 个答案:

答案 0 :(得分:1)

PHP函数glob(here)将为您提供匹配的文件数组。所以你可以做到以下几点:

$files = glob("properties/{$owner}/{$property}/sample*.pdf");

然后,这将返回“properties / {$ owner} / {$ property} /”目录中以“sample”开头的文件数组,扩展名为.pdf

然后,您可以遍历文件并执行您需要的操作

//Check if there are any files and there were not any FATAL errors
if (count($files) > 0 && $files !== FALSE) {
    foreach ($files as $file) {
        //Do something here
    }
} else {
    //There were no matching files
}

希望这有帮助

相关问题