PHP を使用して、特定のフォルダー内の のsample1.pdf
ような名前で始まるファイルの存在を確認する方法を教えてください。sample12.pdf
以下は、単一のファイルをチェックするための私のコードです。
<html>
<?php
if(file_exists("properties/".$owner."/".$property."/sample1.pdf") )
?>
</html>
PHP を使用して、特定のフォルダー内の のsample1.pdf
ような名前で始まるファイルの存在を確認する方法を教えてください。sample12.pdf
以下は、単一のファイルをチェックするための私のコードです。
<html>
<?php
if(file_exists("properties/".$owner."/".$property."/sample1.pdf") )
?>
</html>
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
}
お役に立てれば