简体   繁体   中英

checking existence of file start from name “sample” using php

Could someone help me with how I can check the existance of files which starts with any name like sample1.pdf , sample12.pdf in a particular folder using PHP? Below is my code for checking a single file.

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

The PHP function glob ( here ) will provide you an array of files that match. So you can do the following:

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

This will then return an array of files within the "properties/{$owner}/{$property}/" directory that start with "sample" and have an extension of .pdf

You can then loop through the files and do what you need

//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
}

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM