Hướng dẫn php list only files in directory - php chỉ liệt kê các tệp trong thư mục

Đây là một lớp lót nhanh chóng và đơn giản để chỉ liệt kê các tệp. Vì người dùng chỉ muốn liệt kê các tệp, nên không cần phải quét thư mục và trả về tất cả các nội dung và loại trừ các thư mục. Chỉ cần nhận các tập tin thuộc bất kỳ loại hoặc loại cụ thể. Sử dụng * để trả về tất cả các tệp bất kể phần mở rộng hoặc nhận các tệp có tiện ích mở rộng cụ thể bằng cách thay thế * bằng tiện ích mở rộng.

Nhận tất cả các tệp bất kể phần mở rộng:

$files = glob[$dir . DIRECTORY_SEPARATOR . "*"];

Nhận tất cả các tệp với phần mở rộng PHP:

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];

Nhận tất cả các tệp với tiện ích mở rộng JS:

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];

Tôi sử dụng những thứ sau cho các trang web của mình:

function fileList[string $directory, string $extension=""] :array
{
    $filetype = '*';
    if[!empty[$extension] && mb_substr[$extension, 0, 1, "UTF-8"] != '.']:
        $filetype .= '.' . $extension;
    else:
        $filetype .= $extension;
    endif;
    return glob[$directory . DIRECTORY_SEPARATOR . $filetype];
}

Cách sử dụng :

$files = fileList[$configData->includesDirectory, ''];

Với chức năng tùy chỉnh của tôi, tôi có thể bao gồm một phần mở rộng hoặc để trống. Ngoài ra, tôi có thể quên đặt. Trước khi mở rộng và nó sẽ thành công.

Nerbert ¶

Fatpratmatt Dot tại dot gmail dot com ¶List files and directories inside the specified path

14 năm trước

Simon Dot Riget tại Gmail Dot Com ¶[string $directory, int $sorting_order = SCANDIR_SORT_ASCENDING, ?resource $context =

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
0]: array|false

6 năm trước

telefoontoestel59 tại hotmail dot com

Thư mục sẽ được quét.

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
2

Theo mặc định, thứ tự được sắp xếp theo thứ tự theo thứ tự tăng dần. Nếu

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
2 tùy chọn được đặt thành
$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
4, thì thứ tự sắp xếp là theo thứ tự bảng chữ cái theo thứ tự giảm dần. Nếu nó được đặt thành
$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
5 thì kết quả sẽ không được phân loại.
$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
4
, then the sort order is alphabetical in descending order. If it is set to
$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
5
then the result is unsorted.

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
6

Để biết mô tả về tham số

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
6, hãy tham khảo phần luồng của hướng dẫn.

Trả về giá trị

Trả về một mảng tên tệp thành công, hoặc

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
8 khi thất bại. Nếu
$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
1 không phải là một thư mục, thì boolean
$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
8 được trả về và lỗi của cấp
$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];
1 được tạo ra.array of filenames on success, or
$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
8
on failure. If
$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
1 is not a directory, then boolean
$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
8
is returned, and an error of level
$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];
1
is generated.

Thay đổi

Phiên bảnSự mô tả
8.0.0
$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
6 bây giờ là vô hiệu.

Ví dụ

Ví dụ #1 ví dụ scandir [] đơn giảnscandir[] example

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];
3

Ví dụ trên sẽ xuất ra một cái gì đó tương tự như:

Array
[
    [0] => .
    [1] => ..
    [2] => bar.php
    [3] => foo.txt
    [4] => somedir
]
Array
[
    [0] => somedir
    [1] => foo.txt
    [2] => bar.php
    [3] => ..
    [4] => .
]

Ghi chú

Mẹo

Một URL có thể được sử dụng như một tên tệp với chức năng này nếu các giấy gói fopen đã được bật. Xem fopen [] để biết thêm chi tiết về cách chỉ định tên tệp. Xem các giao thức và trình bao bọc được hỗ trợ để biết các liên kết đến thông tin về những khả năng mà các trình bao bọc khác nhau có, ghi chú về việc sử dụng của chúng và thông tin về bất kỳ biến được xác định trước mà họ có thể cung cấp.fopen[] for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.

Xem thêm

  • opendir [] - tay cầm thư mục mở
  • readDir [] - Đọc mục nhập từ tay cầm thư mục
  • glob [] - Tìm tên đường dẫn khớp với một mẫu
  • is_dir [] - cho biết tên tệp có phải là thư mục
  • Sắp xếp [] - Sắp xếp một mảng theo thứ tự tăng dần

Dwieeb tại gmail dot com

10 năm trước

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];
4

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];
5

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];
6

CoolBikram0 tại Gmail Dot Com ¶

11 thàng trước

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];
7

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];
8

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];
9

mmda dot nl tại gmail dot com ¶

10 năm trước

function fileList[string $directory, string $extension=""] :array
{
    $filetype = '*';
    if[!empty[$extension] && mb_substr[$extension, 0, 1, "UTF-8"] != '.']:
        $filetype .= '.' . $extension;
    else:
        $filetype .= $extension;
    endif;
    return glob[$directory . DIRECTORY_SEPARATOR . $filetype];
}
0

function fileList[string $directory, string $extension=""] :array
{
    $filetype = '*';
    if[!empty[$extension] && mb_substr[$extension, 0, 1, "UTF-8"] != '.']:
        $filetype .= '.' . $extension;
    else:
        $filetype .= $extension;
    endif;
    return glob[$directory . DIRECTORY_SEPARATOR . $filetype];
}
1

function fileList[string $directory, string $extension=""] :array
{
    $filetype = '*';
    if[!empty[$extension] && mb_substr[$extension, 0, 1, "UTF-8"] != '.']:
        $filetype .= '.' . $extension;
    else:
        $filetype .= $extension;
    endif;
    return glob[$directory . DIRECTORY_SEPARATOR . $filetype];
}
2

function fileList[string $directory, string $extension=""] :array
{
    $filetype = '*';
    if[!empty[$extension] && mb_substr[$extension, 0, 1, "UTF-8"] != '.']:
        $filetype .= '.' . $extension;
    else:
        $filetype .= $extension;
    endif;
    return glob[$directory . DIRECTORY_SEPARATOR . $filetype];
}
3

CoolBikram0 tại Gmail Dot Com ¶

11 thàng trước

function fileList[string $directory, string $extension=""] :array
{
    $filetype = '*';
    if[!empty[$extension] && mb_substr[$extension, 0, 1, "UTF-8"] != '.']:
        $filetype .= '.' . $extension;
    else:
        $filetype .= $extension;
    endif;
    return glob[$directory . DIRECTORY_SEPARATOR . $filetype];
}
4

mmda dot nl tại gmail dot com ¶

Thông tin tại Ghi chú chấm không ¶

function fileList[string $directory, string $extension=""] :array
{
    $filetype = '*';
    if[!empty[$extension] && mb_substr[$extension, 0, 1, "UTF-8"] != '.']:
        $filetype .= '.' . $extension;
    else:
        $filetype .= $extension;
    endif;
    return glob[$directory . DIRECTORY_SEPARATOR . $filetype];
}
5

4 năm trước

10 năm trước

function fileList[string $directory, string $extension=""] :array
{
    $filetype = '*';
    if[!empty[$extension] && mb_substr[$extension, 0, 1, "UTF-8"] != '.']:
        $filetype .= '.' . $extension;
    else:
        $filetype .= $extension;
    endif;
    return glob[$directory . DIRECTORY_SEPARATOR . $filetype];
}
6

function fileList[string $directory, string $extension=""] :array
{
    $filetype = '*';
    if[!empty[$extension] && mb_substr[$extension, 0, 1, "UTF-8"] != '.']:
        $filetype .= '.' . $extension;
    else:
        $filetype .= $extension;
    endif;
    return glob[$directory . DIRECTORY_SEPARATOR . $filetype];
}
7

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];
6

CoolBikram0 tại Gmail Dot Com ¶

Thông tin tại Ghi chú chấm không ¶

function fileList[string $directory, string $extension=""] :array
{
    $filetype = '*';
    if[!empty[$extension] && mb_substr[$extension, 0, 1, "UTF-8"] != '.']:
        $filetype .= '.' . $extension;
    else:
        $filetype .= $extension;
    endif;
    return glob[$directory . DIRECTORY_SEPARATOR . $filetype];
}
9

$files = fileList[$configData->includesDirectory, ''];
0

$files = fileList[$configData->includesDirectory, ''];
1

$files = fileList[$configData->includesDirectory, ''];
2

4 năm trước

EEP2004 tại UKR DOT Net

$files = fileList[$configData->includesDirectory, ''];
3

$files = fileList[$configData->includesDirectory, ''];
4

$files = fileList[$configData->includesDirectory, ''];
5

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];
6

8 năm trước

EEP2004 tại UKR DOT Net

$files = fileList[$configData->includesDirectory, ''];
7

$files = fileList[$configData->includesDirectory, ''];
8

$files = fileList[$configData->includesDirectory, ''];
9

Array
[
    [0] => .
    [1] => ..
    [2] => bar.php
    [3] => foo.txt
    [4] => somedir
]
Array
[
    [0] => somedir
    [1] => foo.txt
    [2] => bar.php
    [3] => ..
    [4] => .
]
0

8 năm trước

Kodlee tại Kodleeshare Dot Net

Array
[
    [0] => .
    [1] => ..
    [2] => bar.php
    [3] => foo.txt
    [4] => somedir
]
Array
[
    [0] => somedir
    [1] => foo.txt
    [2] => bar.php
    [3] => ..
    [4] => .
]
1

Array
[
    [0] => .
    [1] => ..
    [2] => bar.php
    [3] => foo.txt
    [4] => somedir
]
Array
[
    [0] => somedir
    [1] => foo.txt
    [2] => bar.php
    [3] => ..
    [4] => .
]
2

Array
[
    [0] => .
    [1] => ..
    [2] => bar.php
    [3] => foo.txt
    [4] => somedir
]
Array
[
    [0] => somedir
    [1] => foo.txt
    [2] => bar.php
    [3] => ..
    [4] => .
]
3

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];
6

gambit_642 tại hotmaildotcom ¶

csaba tại alum dot mit dot edu ¶

Array
[
    [0] => .
    [1] => ..
    [2] => bar.php
    [3] => foo.txt
    [4] => somedir
]
Array
[
    [0] => somedir
    [1] => foo.txt
    [2] => bar.php
    [3] => ..
    [4] => .
]
5

Array
[
    [0] => .
    [1] => ..
    [2] => bar.php
    [3] => foo.txt
    [4] => somedir
]
Array
[
    [0] => somedir
    [1] => foo.txt
    [2] => bar.php
    [3] => ..
    [4] => .
]
6

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];
6

17 năm trước

10 năm trước

Array
[
    [0] => .
    [1] => ..
    [2] => bar.php
    [3] => foo.txt
    [4] => somedir
]
Array
[
    [0] => somedir
    [1] => foo.txt
    [2] => bar.php
    [3] => ..
    [4] => .
]
8

Array
[
    [0] => .
    [1] => ..
    [2] => bar.php
    [3] => foo.txt
    [4] => somedir
]
Array
[
    [0] => somedir
    [1] => foo.txt
    [2] => bar.php
    [3] => ..
    [4] => .
]
9

$directory0

$directory1

$directory2

$directory3

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];
6

CoolBikram0 tại Gmail Dot Com ¶

10 năm trước

$directory5

CoolBikram0 tại Gmail Dot Com ¶

11 thàng trước

$directory6

$directory7

$directory8

$directory9

$sorting_order0

$sorting_order1

$sorting_order2

$sorting_order3

$sorting_order4

$sorting_order5

$sorting_order6

$sorting_order7

mmda dot nl tại gmail dot com ¶

csaba tại alum dot mit dot edu ¶

$sorting_order8

$sorting_order9

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];
6

17 năm trước

Pawel Dlugosz ¶

SCANDIR_SORT_ASCENDING1

SCANDIR_SORT_ASCENDING2

SCANDIR_SORT_ASCENDING3

SCANDIR_SORT_ASCENDING4

SCANDIR_SORT_ASCENDING5

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];
6

Stan P. van de Burgt ¶

18 năm trước

SCANDIR_SORT_ASCENDING7

SCANDIR_SORT_ASCENDING8

SCANDIR_SORT_ASCENDING9

$context0

Artmanniako tại Gmail Dot Com ¶

3 năm trước

$context1

$context2

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];
6

Fazle Dot Elahee tại Gmail Dot Com ¶

3 năm trước

$context4

$context5

$context6

$context7

Fazle Dot Elahee tại Gmail Dot Com ¶

Thông tin tại Ghi chú chấm không ¶

$context8

$context9

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
00

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
01

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
02

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
03

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
04

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
05

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];
6

4 năm trước

10 năm trước

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
07

CoolBikram0 tại Gmail Dot Com ¶

11 thàng trước

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
08

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
09

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];
6

mmda dot nl tại gmail dot com ¶

Thông tin tại Ghi chú chấm không ¶

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
11

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
12

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
13

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];
6

4 năm trước

11 thàng trước

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
15

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
16

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
17

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];
6

mmda dot nl tại gmail dot com ¶

EEP2004 tại UKR DOT Net

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
19

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
20

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
21

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
22

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];
6

8 năm trước

11 thàng trước

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
24

mmda dot nl tại gmail dot com ¶

Thông tin tại Ghi chú chấm không ¶

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
25

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
26

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
27

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
28

4 năm trước

EEP2004 tại UKR DOT Net

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
29

8 năm trước

csaba tại alum dot mit dot edu ¶

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
30

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
31

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];
6

17 năm trước

11 thàng trước

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
33

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
34

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
35

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
36

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];
6

mmda dot nl tại gmail dot com ¶

Thông tin tại Ghi chú chấm không ¶

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
38

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
39

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
40

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
41

4 năm trước

11 thàng trước

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
42

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
43

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
44

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
45

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
46

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];
6

mmda dot nl tại gmail dot com ¶

11 thàng trước

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
48

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
49

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
50

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];
6

mmda dot nl tại gmail dot com ¶

Pawel Dlugosz ¶

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
52

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
53

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
54

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
55

4 năm trước

EEP2004 tại UKR DOT Net

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
56

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
57

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];
6

8 năm trước

Kodlee tại Kodleeshare Dot Net

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
59

gambit_642 tại hotmaildotcom ¶

EEP2004 tại UKR DOT Net

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
60

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.php"];
61

$files = glob[$dir . DIRECTORY_SEPARATOR . "*.js"];
6

__ dir __ trong PHP là gì?

__Dir__ có thể được sử dụng để có được thư mục làm việc mã hiện tại. Nó đã được giới thiệu trong PHP bắt đầu từ phiên bản 5.3. Nó tương tự như sử dụng dirname [__ file__]. Thông thường, nó được sử dụng để bao gồm các tệp khác có trong một tệp được bao gồm.used to obtain the current code working directory. It has been introduced in PHP beginning from version 5.3. It is similar to using dirname[__FILE__]. Usually, it is used to include other files that is present in an included file.

Làm thế nào bao gồm tất cả các tệp trong một thư mục trong PHP?

Để bao gồm tất cả các tệp PHP từ thư mục cùng một lúc cần một vòng lặp foreach ...
Ví dụ: Ví dụ này chứa bốn tệp PHP [File1. ...
Tạo File1.php trong thư mục 'MyGeek':.
Tạo File2.php trong thư mục 'MyGeeks':.
Tạo File3.php trong thư mục 'MyGeeks':.
Tạo File4.php trong thư mục 'MyGeeks':.
Tạo Main.php bên ngoài thư mục:.

Làm thế nào tôi có thể nhận được một danh sách tất cả các thư mục con và tệp có trong một thư mục bằng PHP?

PHP sử dụng scandir [] để tìm các thư mục trong thư mục hàm scandir là một hàm sẵn có trả về một loạt các tệp và thư mục của một thư mục cụ thể.Nó liệt kê các tệp và thư mục có bên trong đường dẫn được chỉ định bởi người dùng.scandir[] to find folders in a directory The scandir function is an inbuilt function that returns an array of files and directories of a specific directory. It lists the files and directories present inside the path specified by the user.

PHP có phải là một thư mục không?

Hàm is_dir [] trong PHP được sử dụng để kiểm tra xem tệp được chỉ định có phải là thư mục hay không.Tên của tệp được gửi dưới dạng tham số cho hàm is_dir [] và nó trả về true nếu tệp là một thư mục khác, nó trả về sai.Các tham số được sử dụng: Hàm is_dir [] trong PHP chỉ chấp nhận một tham số.. The name of the file is sent as a parameter to the is_dir[] function and it returns True if the file is a directory else it returns False. Parameters Used: The is_dir[] function in PHP accepts only one parameter.

Bài Viết Liên Quan

Chủ Đề