Hướng dẫn read folder in php

I want to read a list the names of files in a folder in a web page using php. is there any simple script to acheive it?

asked Apr 6, 2009 at 9:18

Arun AnnamalaiArun Annamalai

7351 gold badge7 silver badges18 bronze badges

0

The simplest and most fun way [imo] is glob

foreach [glob["*.*"] as $filename] {
    echo $filename."
"; }

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."
"; } closedir[$dh]; } }

There are also the SPL DirectoryIterator methods. If you are interested

diEcho

52.5k41 gold badges172 silver badges241 bronze badges

answered Apr 6, 2009 at 9:20

Ólafur WaageÓlafur Waage

67.2k19 gold badges140 silver badges194 bronze badges

3

This is what I like to do:

$files = array_values[array_filter[scandir[$path], function[$file] use [$path] { 
    return !is_dir[$path . '/' . $file];
}]];

foreach[$files as $file]{
    echo $file;
}

answered Oct 29, 2014 at 13:57

Bruno CalzaBruno Calza

2,7141 gold badge22 silver badges25 bronze badges

1

There is this function scandir[]:

$dir = 'dir';
$files = scandir[$dir, 0];
for[$i = 2; $i < count[$files]; $i++]
    print $files[$i]."
";

More here in the php.net manual

answered Oct 29, 2014 at 13:04

If you have problems with accessing to the path, maybe you need to put this:

$root = $_SERVER['DOCUMENT_ROOT'];
$path = "/cv/"; 

// Open the folder
 $dir_handle = @opendir[$root . $path] or die["Unable to open $path"];

answered Apr 29, 2013 at 18:37

MontDeskaMontDeska

1,57718 silver badges16 bronze badges

Check in many folders :

Folder_1 and folder_2 are name of folders, from which we have to select files.

$format is required format.


answered Jan 11, 2015 at 9:04

RishabhRishabh

4865 silver badges8 bronze badges

You can use standard directory functions

$dir = opendir['/tmp'];
while [$file = readdir[$dir]] {
    if [$file == '.' || $file == '..'] {
        continue;
    }

    echo $file;
}
closedir[$dir];

answered Jan 20, 2016 at 10:47

Vlad MillerVlad Miller

2,1261 gold badge17 silver badges34 bronze badges



Names




 


Enter Name:



answered Dec 3, 2016 at 19:40

1

Not the answer you're looking for? Browse other questions tagged php file or ask your own question.

Chủ Đề