Hongmu Notes
Home Language Notes PHP reads files with a specified file extension from a given directory and outputs their filenames in order based on the time the files were created.
Language Notes PHP

PHP reads files with a specified file extension from a given directory and outputs their filenames in order based on the time the files were created.

PHP reads files with a specified file extension from a given directory and outputs their filenames in order based on the time the files were created.

You can use PHP's built-in functions. glob() Retrieve files with a specified extension from a designated directory, then use them. filectime() The function retrieves the creation time of each file and stores the file names along with their creation times in an array; then it is used. usort() The function sorts the array based on its creation time; then, it simply iterates through the array and prints the file names.

Here is an example code snippet:

<?php
// 指定目录和后缀
$dir = '/path/to/directory/';
$extension = 'txt';

// 获取指定后缀的文件
$files = glob($dir . '*.' . $extension);

// 保存文件名和创建时间的数组
$file_list = array();

// 遍历文件
foreach ($files as $file) {
    // 获取文件的创建时间
    $ctime = filectime($file);

    // 将文件名和创建时间保存到数组中
    $file_list[] = array(
        'name' => $file,
        'ctime' => $ctime
    );
}

// 按照创建时间排序
usort($file_list, function($a, $b) {
    return $a['ctime'] - $b['ctime'];
});

// 输出文件名
foreach ($file_list as $file) {
    echo $file['name'] . PHP_EOL;
}
?>

In the above example code, the relevant parts need to be replaced. $dir$extension The variables represent the directory path and file extension for the file to be read. Use. glob() The function retrieves files with a specified suffix, then iterates through these files to extract their creation times, and stores both the file names and their creation times into an array. Finally, it is used. usort() The function sorts an array based on the creation time and then iterates through the array to output the file names.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP ob function record

PHP ob function record Language Notes PHP

Usage of the following three functions ob_get_contents(); ob_end_clean(); ob_start(); You can use these functions to buffer local files and execute local script code. Use ob_start() to save the output code into the buffer, and the page will not be displayed; then use ob_get_contents to get the data in the buffer. o…
👁 143
PHP preg

PHP preg Language Notes PHP

The preg_match_all function is used to perform a global regular expression match. preg_match_all() Syntax int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG…
👁 169
Detailed explanation of PHP ternary operator and if

Detailed explanation of PHP ternary operator and if Language Notes PHP

Ternary operator condition ? Result 1 : Result 2 Explanation: The position in front of the question mark is the condition for judgment. If the condition is met, the result is 1, and if it is not met, the result is 2. This article compares and explains the ternary operator and if...else... in detail. I hope it will be helpful to everyone. Today when I was revising my paper online, I encountered a statement that I couldn’t understand: $if_summary = $row['IF_SUMMARY']=…
👁 191
PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232

Recommended reading

(PC+WAP) Marketing-Focused Forklift and Construction Machinery Website – pbootCMS Template; Red Machinery Manufacturing Website Source Code Download – 0100

(PC+WAP) Marketing-Focused Forklift and Construction Machinery Website – pbootCMS Template; Red Machinery Manufacturing Website Source Code Download – 0100 Practical Collection pbootcms Template

This PbootCMS template features a red color scheme and is specifically designed for marketing-oriented forklift and construction machinery enterprises, supporting both PC and WAP devices. Its sophisticated and professional design effectively showcases products such as forklifts and loaders. The template includes built-in Product Center and Case Study display modules, helping construction machinery companies boost their online sales conversion rates. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin...
👁 46
(PC+WAP) Livestock Farming Equipment Website Template – Download Automated Equipment Website Source Code (1049)

(PC+WAP) Livestock Farming Equipment Website Template – Download Automated Equipment Website Source Code (1049) Practical Collection pbootcms Template

A PbootCMS website template for livestock farming and automation equipment, compatible with both PC and WAP devices. The professional and practical design is ideal for showcasing livestock farming equipment, automation systems, and project cases. It helps livestock equipment manufacturers showcase their products online and attract farm operators and agricultural clients. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4...
👁 41
Environmental Protection & Energy-Saving Smart Air Purifier Website Template – 0868

Environmental Protection & Energy-Saving Smart Air Purifier Website Template – 0868 Practical Collection Yiyou template

An eyouCMS website template designed for enterprises specializing in eco-friendly, energy-efficient smart air purifiers. Featuring a green, tech-driven design style, this template effectively showcases smart purification products, energy-saving technologies, and customized solutions. It helps environmental technology companies present their brands online and attract both residential and corporate clients. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EasyCMS...
👁 44
(PC+WAP) Marketing-oriented plastic sheet purification and eco-friendly equipment website – pbootCMS template; Green and eco-friendly hardware sheet website template download – 0101

(PC+WAP) Marketing-oriented plastic sheet purification and eco-friendly equipment website – pbootCMS template; Green and eco-friendly hardware sheet website template download – 0101 Practical Collection pbootcms Template

A PbootCMS marketing template designed for the plastic sheet, environmental protection equipment, and hardware sheet industries, featuring a green-themed design and supporting both PC and WAP devices. The page layout focuses on product showcasing and environmental sustainability messaging, helping companies specializing in eco-friendly materials build a green brand image online and attract targeted customers. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www...
👁 60
(Adaptive Mobile Version) Advertising Agency Website Template; Advertising Design Agency Website Source Code – 1050

(Adaptive Mobile Version) Advertising Agency Website Template; Advertising Design Agency Website Source Code – 1050 Practical Collection pbootcms Template

A PbootCMS website template designed for advertising and cultural agencies as well as advertising design firms, supporting mobile access. Its creative and visually appealing design makes it ideal for these agencies to showcase their brand strategy, design works, and case studies. This template helps advertising agencies demonstrate their professional expertise online and attract brand clients. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.c...
👁 49
Responsive whole-house custom furniture website template – 0869

Responsive whole-house custom furniture website template – 0869 Practical Collection Yiyou template

This EYO CMS responsive template is ideal for the whole-house custom furniture industry. Its premium, modern design style effectively showcases whole-house custom furniture products, design projects, and brand strength. It helps custom furniture brands attract homeowners online and enhance their brand image. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Questions for EYO CMS | EYO CMS (Eyo...
👁 50