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
List all files in one directory PHP
Check this out : readdir()
This bit of code should list all entries in a certain directory:
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "$entry\n";
}
}
closedir($handle);
}
Edit: miah's solution is much more elegant than mine, you should use his solution instead.
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()
Getting the names of all files in a directory with PHP
Don't bother with open/readdir and use glob
instead:
foreach(glob($log_directory.'/*.*') as $file) {
...
}
How to list files and folder in a dir (PHP)
PHP 5 has the RecursiveDirectoryIterator
.
The manual has a basic example:
<?php
$directory = '/system/infomation/';
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
while($it->valid()) {
if (!$it->isDot()) {
echo 'SubPathName: ' . $it->getSubPathName() . "\n";
echo 'SubPath: ' . $it->getSubPath() . "\n";
echo 'Key: ' . $it->key() . "\n\n";
}
$it->next();
}
?>
Edit -- Here's a slightly more advanced example (only slightly) which produces output similar to what you want (i.e. folder names then files).
// Create recursive dir iterator which skips dot folders
$dir = new RecursiveDirectoryIterator('./system/information',
FilesystemIterator::SKIP_DOTS);
// Flatten the recursive iterator, folders come before their files
$it = new RecursiveIteratorIterator($dir,
RecursiveIteratorIterator::SELF_FIRST);
// Maximum depth is 1 level deeper than the base folder
$it->setMaxDepth(1);
// Basic loop displaying different messages based on file or folder
foreach ($it as $fileinfo) {
if ($fileinfo->isDir()) {
printf("Folder - %s\n", $fileinfo->getFilename());
} elseif ($fileinfo->isFile()) {
printf("File From %s - %s\n", $it->getSubPath(), $fileinfo->getFilename());
}
}
PHP: How to list files in a directory without listing subdirectories
It should be like this, I think:
$files = scandir($dir);
foreach($files as $file)
{
if(is_file($dir.$file)){
....
Listing out files in PHP (ONLY the files) as links from all subdirectories of a directory
Try using glob():
<?php
foreach(glob('path-to/UPLOAD_FOLDER/*') as $subDirectory) {
foreach(glob($subDirectory'./*') as $file) {
// do what you want with file here... example:
$file_list .= $file.'<br>';
}
}
echo $file_list;
?>
How to read a list of files from a folder using PHP?
The simplest and most fun way (imo) is glob
foreach (glob("*.*") as $filename) {
echo $filename."<br />";
}
But the standard way is to use the directory functions.
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo "filename: .".$file."<br />";
}
closedir($dh);
}
}
There are also the SPL DirectoryIterator methods. If you are interested
Related Topics
Facebook Graph API, How to Get Users Email
Why Are My Cookies Not Setting
How to Resolve Curl Error (7): Couldn't Connect to Host
Simple Postgresql Statement - Column Name Does Not Exists
How to Loop Through a MySQL Result Set More Than Once Using the MySQL_* Functions
Formatting Datetime Object, Respecting Locale::Getdefault()
How to Give PHP Write Access to a Directory
Dollar ($) Sign in Password String Treated as Variable
PHP Date() Format When Inserting into Datetime in MySQL
How to Run PHP Code When a User Clicks on a Link
Finding the Subsets of an Array in PHP
How to Get the Query Executed in Laravel 5? Db::Getquerylog() Returning Empty Array
"[Notice] Child Pid Xxxx Exit Signal Segmentation Fault (11)" in Apache Error.Log
Unformat Money When Parsing in PHP
Best Way to Manage Long-running PHP Script
How to Re-Index All Subarray Elements of a Multidimensional Array