Just use the DirectoryIterator class.
foreach [new DirectoryIterator['../pictures/uploads'] as $fileInfo] {
if [$fileInfo->isDot[]] continue;
if [stripos[$fileInfo->getFilename[], 'profile' . $id] !== false] {
var_dump[$fileInfo->getExtension[]];
}
}
The DirectoryIterator class is available since PHP 5.3.6.
A more detailed example could be a DirectoryIterator instance used by a FilterIterator instance to just output the desired files. Th given example requires a minimum of PHP 7.4. If you 're using a PHP version smaller than 7.4 remove the type hints for the class properties.
class MyFileFilter extends FilterIterator
{
protected string $filename;
public function __construct[Iterator $iterator, $filename]
{
$this->filename = $filename;
parent::__construct[$iterator];
}
public function accept[]: bool
{
$info = $this->getInnerIterator[];
return stripos[$info->getFilename[], $this->filename] !== false;
}
}
The FilterIterator class is available since PHP 5.1. The above shown extension takes an Iterator and searches for a given filename and returns the SplFileInfo object, if the filename matches.
How to use it:
$directory = new DirectoryIterator['../pictures/uploads'];
$filename = 'picture' . $id;
$filter = new MyFileFilter[$directory, $filename];
foreach [$filter as $file] {
var_dump[$file->getExtension[]];
}
The foreach loop is basically the filter, that only returns files, that match the given filename. Every returned file is a SplFileInfo instance. With this you can use the SplFileInfo::getExtension[] method to get the file extension.
Last but not least comes the GlobIterator class which is available since PHP 5.3. It 's the easiest way to iteratate over a given path with placeholders.
$filesystem = new GlobIterator['../pictures/uploads/profile' . $id . '.*'];
foreach [$filesystem as $file] {
var_dump[$file->getExtension[]];
}
[PHP 5, PHP 7, PHP 8]
scandir — List files and directories inside the specified path
Description
scandir[string $directory
, int $sorting_order
= SCANDIR_SORT_ASCENDING
, ?resource $context
=
null
]: array|false
Parameters
directory
The directory that will be scanned.
sorting_order
By default, the sorted order is alphabetical in ascending order. If the optional sorting_order
is set to SCANDIR_SORT_DESCENDING
, then the sort order is alphabetical in descending order. If it is set to
SCANDIR_SORT_NONE
then the result is unsorted.
context
For a description of the context
parameter, refer to the streams section of the manual.
Return Values
Returns an array of filenames on success, or false
on failure. If directory
is not a directory,
then boolean false
is returned, and an error of level E_WARNING
is generated.
Changelog
8.0.0 | context is now nullable.
|
Examples
Example #1 A simple scandir[] example
The above example will output something similar to:
Array [ [0] => . [1] => .. [2] => bar.php [3] => foo.txt [4] => somedir ] Array [ [0] => somedir [1] => foo.txt [2] => bar.php [3] => .. [4] => . ]
Notes
Tip
A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen[] for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.
See Also
- opendir[] - Open directory handle
- readdir[] - Read entry from directory handle
- glob[] - Find pathnames matching a pattern
- is_dir[] - Tells whether the filename is a directory
- sort[] - Sort an array in ascending order
dwieeb at gmail dot com ¶
10 years ago
Easy way to get rid of the dots that scandir[] picks up in Linux environments:
coolbikram0 at gmail dot com ¶
9 months ago
A simple recursive function to list all files and subdirectories in a directory:
mmda dot nl at gmail dot com ¶
9 years ago
Here is my 2 cents. I wanted to create an array of my directory structure recursively. I wanted to easely access data in a certain directory using foreach. I came up with the following:
Output
Array
[
[subdir1] => Array
[
[0] => file1.txt
[subsubdir] => Array
[
[0] => file2.txt
[1] => file3.txt
]
]
[subdir2] => Array
[
[0] => file4.txt
}
]
info at remark dot no ¶
4 years ago
Someone wrote that array_slice could be used to quickly remove directory entries "." and "..". However, "-" is a valid entry that would come before those, so array_slice would remove the wrong entries.
eep2004 at ukr dot net ¶
7 years ago
Fastest way to get a list of files without dots.