Depending on your constraints, you may be able to generate more concise code at the cost of it being less cross-platform portable, by using the exec()
call. I prefer giving absolute paths, which I usually hard-code in variables in a configuration file included in all scripts, so:
define (FULL_DIRECTORY_PATH,'/whatever/your/path/is/');
...
exec('rm ' . FULL_DIRECTORY_PATH . 'US_A.2.6.*');
Or
define (BASE_PATH,'/base/path/');
...
exec('rm ' . BASE_PATH . 'additional/path/US_A.2.6.*');
This solution would work on linux and other UNIX systems. Although it's uncommon to use PHP on Windows, you could adapt it to work on Windows servers.
There are some critical security risks associated with using exec()
if there is any user-submitted input; with the code as-is, it will be fine as the paths and filenames are all hardcoded. But if you include any custom input, make sure to use the escapeshellarg function.
As George Cummins notes, this solution doesn't include error handling. I find his solution is better if you need error handling on a file-by-file basis. However, I commonly use setups like the one here, with no error handling, for cleanup of automatically generated files, and I have never run into any problems in 10+ years of using such constructs. For such uses, error handling is often only needed during setup and testing, and it usually comes down to setting directory permissions correctly.