Php: Get List of All Filenames Contained Within My Images Directory

PHP: Get list of all filenames contained within my images directory

Try glob

Something like:

 foreach(glob('./images/*.*') as $filename){
echo $filename;
}

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 get the file name under a folder?

You can use the glob() function:

Example 01:

<?php
// read all files inside the given directory
// limited to a specific file extension
$files = glob("./ABC/*.txt");
?>

Example 02:

<?php
// perform actions for each file found
foreach (glob("./ABC/*.txt") as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}
?>

Example 03: Using RecursiveIteratorIterator

<?php 
foreach(new RecursiveIteratorIterator( new RecursiveDirectoryIterator("../")) as $file) {
if (strtolower(substr($file, -4)) == ".txt") {
echo $file;
}
}
?>

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.

Listing all images in a directory using PHP

I like PHP's glob function.

foreach(glob(IMAGEPATH.'*') as $filename){
echo basename($filename) . "\n";
}

Get filenames of images in a directory

It's certainly possible. Have a look at the documentation for opendir and push every file to a result array. If you're using PHP5, have a look at DirectoryIterator. It is a much smoother and cleaner way to traverse the contents of a directory!

EDIT: Building on opendir:

$dir = "/etc/php5/";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
$images = array();

while (($file = readdir($dh)) !== false) {
if (!is_dir($dir.$file)) {
$images[] = $file;
}
}

closedir($dh);

print_r($images);
}
}

Get all filenames from directory via PHP script

You need to use PHP scandir function

Example

<?php
$dir = "/images/";

// Sort in ascending order - this is default
$a = scandir($dir);

// Sort in descending order
$b = scandir($dir,1);

print_r($a);
//Array ( [0] => . [1] => .. [2] => cat.gif [3] => dog.gif [4] => horse.gif [5] => myimages )
echo ($a[4]);
// horse.gif

?>

You can't echo an Array. If you use print_r instead it will print it with the syntax as above

PHP - find all all files within directory that match certain string and put in array

glob('62115465*');

note the removal of the .. glob() essentially replicates doing something like dir *.txt at a command prompt.



Related Topics



Leave a reply



Submit