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.

354 questions
51
votes
7 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
  • 17,311
  • 33
  • 107
  • 166
45
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
  • 970
  • 3
  • 12
  • 27
33
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,481
  • 14
  • 69
  • 132
29
votes
9 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
  • 381
  • 2
  • 4
  • 11
21
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
  • 211
  • 1
  • 2
  • 3
15
votes
7 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
    • 479
    • 1
    • 5
    • 22
    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
    • 409
    • 4
    • 6
    • 18
    12
    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
    • 881
    • 5
    • 21
    • 38
    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
    10
    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,849
    • 4
    • 24
    • 38
    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
    • 590
    • 2
    • 8
    • 23
    7
    votes
    1 answer

    Why is os.scandir() as slow as os.listdir()?

    I tried to optimize a file browsing function written in Python, on Windows, by using os.scandir() instead of os.listdir(). However, time remains unchanged, about 2 minutes and a half, and I can't tell why. Below are the functions, original and…
    Vlad Iordache
    • 468
    • 1
    • 5
    • 16
    7
    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 =…
    Dgeorgex000000
    • 93
    • 1
    • 2
    • 8
    6
    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
    • 105
    • 7
    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
    1
    2 3
    23 24