1

PHP を使用して、特定のフォルダー内の のsample1.pdfような名前で始まるファイルの存在を確認する方法を教えてください。sample12.pdf以下は、単一のファイルをチェックするための私のコードです。

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

1 に答える 1

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
}

お役に立てれば

于 2012-11-21T10:50:34.000 に答える