Best Way to Get Files from a Dir Filtered by Certain Extension in PHP

Best way to get files from a dir filtered by certain extension in php

PHP has a great function to help you capture only the files you need. Its called glob()

glob - Find pathnames matching a pattern

Returns an array containing the matched files/directories, an empty array if no file matched or FALSE on error.

Here is an example usage -

$files = glob("/path/to/folder/*.txt");

This will populate the $files variable with a list of all files matching the *.txt pattern in the given path.

Reference -

  • glob()

Filter by extensions and file names

<?php
$folder = scandir($path);
$files = array();
foreach($folder as $file)
{
if (!in_array($file, array('.', '..', 'index.htm', 'index.html')) && !in_array(substr($file, -4), array('.jpg', '.mp3', '.vtr')))
{
$files[$file] = filemtime($path.'/'.$file);
}
}
?>

Just add the excluded extensions in the second in_array(), as long as the extension name is 3 characters long.

If you want to exclude files with a extension that is 4 characters long, add another in_array. For example :

<?php
$folder = scandir($path);
$files = array();
foreach($folder as $file)
{
if (!in_array($file, array('.', '..', 'index.htm', 'index.html')) && !in_array(substr($file, -4), array('.jpg', '.mp3', '.vtr')) && !in_array(substr($file, -5), array('.jpeg', '.mpeg')))
{
$files[$file] = filemtime($path.'/'.$file);
}
}
?>

The substr will extract the extension name, and !in_array will make sure the extension name is not in the given list of extensions.

PHP list of specific files in a directory

if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != ".." && strtolower(substr($file, strrpos($file, '.') + 1)) == 'xml')
{
$thelist .= '<li><a href="'.$file.'">'.$file.'</a></li>';
}
}
closedir($handle);
}

A simple way to look at the extension using substr and strrpos

Find files with a given extension in a directory tree

You could use a RecursiveDirectoryIterator to get all files recursively, then filter the result with a RegexIterator to iterate over only the *.html files. To get the relative adress, RecursiveDirectoryIterator::getSubPathname() can be used.

$directory  = '../path/to/your/folder';
$all_files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
$html_files = new RegexIterator($all_files, '/\.html$/');

foreach($html_files as $file) {
echo $html_files->getSubPathname(), PHP_EOL;
}

If you really need and array (with iterable objects you often don't), you can easily build one up in the foreach rather than echo-ing each subpath.

Get files from SFTP folder filtered by suffix/extension with phpseclib nlist

SFTP does not allow filtering of files (the protocol itself, it's not a limitation of SFTP implementation in PHP).

So all you can do is to retrieve listing of a whole directory and filter them locally.

$files = $sftp->nlist($sftp_path);

foreach ($files as $file)
{
if (preg_match("/\.txt$/i", $file))
{
echo "Found $file\n";
}
}

You can of course, use SSH "exec" channel to execute ls *.txt on the server. But that's not an SFTP solution anymore, you need to have a shell access to the server.

$filelist = $ssh->exec("ls *.txt");

Best Way to Retrieve File Names in a Directory with a Specified Extension in PHP

I would use glob. Maybe the most simplistic approach to this:

$files = glob( dirname(__FILE__) . '/*.php'); 
$files = array_map('basename', $files);
print_r($files);


Related Topics



Leave a reply



Submit