简体   繁体   中英

I am trying to use php file() and pull from another directory and use an * in the filename

I am trying to use the file () function to read a file into an array from another directory. The directory would be at ../data. Also need to read the file in as anything beginning with PaymentPlanExport*. I will be reading these in monthly and only one file will be in that directory at a time. The only difference between the file are the date.

$records = file("PaymentPlanExport*");

file doesn't support wildcards. If you need wildcard support for file operations in php, you could use the glob function, which returns a array of all matching file.

In your case, you could simply use file(glob('PaymentPlanExport*')[0]) - but I would recommend to add some checks, if the file really exists.

$pattern = '../data/PaymentPlanExport*';
$files = glob($pattern);
if (count($files) != 1) {
    // error handling
}
$f = fopen($files[0]);

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