Questions tagged [scandir]

List files and directories inside the specified path

PHP usage:

array scandir ( string $directory [, int $sorting_order = SCANDIR_SORT_ASCENDING [, resource $context ]] )

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.

338 questions
46
votes
6 answers

How to get only images using scandir in PHP?

Is there any way to get only images with extensions jpeg, png, gif etc while using $dir = '/tmp'; $files1 = scandir($dir);
esafwan
  • 14,622
  • 30
  • 99
  • 154
40
votes
11 answers

Exclude hidden files from scandir

I am using the following code to get a list of images in a directory: $files = scandir($imagepath); but $files also includes hidden files. How can I exclude them?
Sino
  • 870
  • 3
  • 12
  • 27
30
votes
4 answers

scandir() to sort by date modified

I'm trying to make scandir(); function go beyond its written limits, I need more than the alpha sorting it currently supports. I need to sort the scandir(); results to be sorted by modification date. I've tried a few solutions I found here and some…
aborted
  • 4,504
  • 11
  • 59
  • 118
26
votes
6 answers

Include JUST files in scandir array?

I have an array I'm getting back from scandir, but it contains "." and ".." and I don't want it to. My code: $indir = scandir('../pages'); $fileextensions = array(".", "php", "html", "htm", "shtml"); $replaceextensions = str_replace($fileextensions,…
Rbn
  • 341
  • 2
  • 4
  • 11
20
votes
3 answers

PHP most efficient way to list the files in a very large directory

Possible Duplicates: Get the Files inside a directory PHP: scandir() is too slow I have a directory with tens of thousands of files in it and I want to display a list of these files on a page. I tried doing it with scandir and it takes forever. …
John
  • 201
  • 1
  • 2
  • 3
13
votes
3 answers

How to sort files in some directory by the names on Linux

I use opendir() and readdir() to display the file names in a directory. But they are disordered. How can I sort them? The language is C.
JavaMobile
  • 411
  • 3
  • 6
  • 18
12
votes
2 answers

with os.scandir() raises AttributeError: __exit__

An AttributeError is raised when I use the example code from python's documentation (here). The example code is as follows: with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): …
andrew_berge
  • 123
  • 1
  • 5
9
votes
4 answers

How to properly use scandir() in c?

I am trying to store list of files in a char** variable. scandir() finishes properly but I get a segmentation fault when trying to print the char**. Here's the code: int main() { char** fileList; int noOfFiles; char* path = "."; …
kBisla
  • 550
  • 2
  • 6
  • 22
8
votes
3 answers

scandir - sort numeric filenames

Done some searching, but can't seem to find the exact answer I'm looking for. I'd like to pull in files with numbered filenames using 'scandir($dir)', but have them sort properly. For example, file names…
Phil
  • 1,332
  • 4
  • 19
  • 33
8
votes
2 answers

PHP: scandir() is too slow

I have to make a function that lists all subfolders into a folder. I have a no-file filter, but the function uses scandir() for listing. That makes the application very slow. Is there an alternative of scandir(), even a not native php…
Emil Avramov
  • 753
  • 5
  • 16
  • 32
8
votes
4 answers

PHP scandir recursively

I'd like my script to scandir recursively, $files = scandir('/dir'); foreach($files as $file){ if(is_dir($file)){ echo '
  • '; $subfiles =…
  • GRX
    • 390
    • 1
    • 3
    • 21
    6
    votes
    2 answers

    Reading *.csv files from directory and showing the content of each file fails

    I need help with reading the folder and opening / outputting the csv data, I have used examples other people have written but none of them have worked for me. What I currently have is this, but it doesn't output the files: $files =…
    6
    votes
    3 answers

    PHP RecursiveDirectoryIterator

    I want to do a RecursiveDirectoryIterator on a set of folders in a directory, say ./temp and then list the files in each folder according to the name of the folder. For example I have the folders A and B. In A, I have a list of files say, 1.txt,…
    Ameque
    • 61
    • 1
    • 1
    • 6
    5
    votes
    1 answer

    PHP echo all subfolder images

    I have a directory with subfolders containing images. I need to display all these on one page, and also their folder name, so something like this: echo Subfolder name echo image, image, image echo Subfolder2 name echo image2, image2 ,…
    John K
    • 95
    • 7
    5
    votes
    5 answers

    PHP: Using scandir(), folders are treated as files

    Using PHP 5.3.3 (stable) on Linux CentOS 5.5. Here's my folder structure: www/myFolder/ www/myFolder/testFolder/ www/myFolder/testFile.txt Using scandir() against the "myFolder" folder I get the following results: . .. testFolder testFile.txt I'm…
    Reado
    • 1,221
    • 5
    • 20
    • 49
    1
    2 3
    22 23