Hongmu Notes
Home Language Notes PHP: Query files with a specified extension within a given directory.
Language Notes PHP

PHP: Query files with a specified extension within a given directory.

PHP: Query files with a specified extension within a given directory.

You can use PHP's `glob()` function to search for files with a specific file extension within a given directory. The `glob()` function accepts a wildcard pattern, which you can use to match file names in the directory.

The following is an example code that demonstrates how to use the `glob` function to retrieve all PHP files within a specified directory:

<?php
$dir = "/path/to/directory"; // 指定目录
$extension = "*.php"; // 指定后缀名

// 使用glob函数查询指定目录下的文件
foreach(glob($dir . '/' . $extension) as $file) {
    echo $file . "\n";
}
?>

This code first assigns the directory path and file extension to the variables $dir and $extension, respectively. Then, it uses the `glob` function to retrieve all files in the specified directory that match the given file extension, and employs a `foreach` loop to output the full path of each file.

You can modify the $dir and $extension variables as needed to query different directories and file types.

 
微信赞赏

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…
👁 141
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']=…
👁 189
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

Responsive Photo Album Photography Website Template 0941

Responsive Photo Album Photography Website Template 0941 Practical Collection Yiyou template

This eyouCMS responsive template is ideal for the album, portrait photography, and photography industries. Its visually artistic design is perfect for showcasing photographic works, photo albums, album designs, and photography services. It helps photography studios showcase their work online and attract clients. Template Demo | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (Ey...
👁 50
Financial Registration Company Business Service Website Template 0135

Financial Registration Company Business Service Website Template 0135 Practical Collection Yiyou template

This EyouCMS template is ideal for the financial registration and corporate business registration services industry. Its professional and sophisticated design effectively showcases business registration, financial agency services, qualification application services, and success stories, helping business service providers attract startup clients online. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 27
(PC + WAP) Media & Cultural Advertising Website – pbootCMS Template; Download a professional film and television company website source code – 0526

(PC + WAP) Media & Cultural Advertising Website – pbootCMS Template; Download a professional film and television company website source code – 0526 Practical Collection pbootcms Template

A sophisticated PbootCMS website template designed for media, cultural, and film/TV advertising companies, supporting both PC and WAP access. Its artistic design and strong visual impact make it ideal for showcasing film and television productions, advertising campaigns, and cultural events. The template includes built-in modules such as video galleries, news updates, and team profiles, helping cultural and media enterprises build a professional brand image. Template Display | Installation Instructions | Website Backend: /admin.php | Username: ad...
👁 56
Responsive scrolling photo album website template 0942

Responsive scrolling photo album website template 0942 Practical Collection Yiyou template

An EYO CMS responsive website template designed for the scrolling gallery and photo album industry. Its creative visual design and full-screen scrolling effect vividly showcase photographic works and brand stories. This template helps photography agencies present their premium brand image online and attract customers. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EYO CMS | EYO CMS (Eyo...
👁 38
QQ Personal Space – Blog/News Website Template 0136

QQ Personal Space – Blog/News Website Template 0136 Practical Collection Yiyou template

An eYouCMS template designed for QQ Personal Space, blog/chronicle websites, and news/information platforms. Its fresh and artistic design makes it ideal for hosting emotional journals, personalized signatures, engaging blog posts, and real-time updates. This template helps individual webmasters or bloggers build personalized content-sharing platforms. Template Preview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for eYouCMS | eYou...
👁 29
(Adaptive mobile version) Bilingual (Chinese/English) mechanical equipment website – PBootCMS template; Download source code for a general-purpose HTML5 machinery website – 0527

(Adaptive mobile version) Bilingual (Chinese/English) mechanical equipment website – PBootCMS template; Download source code for a general-purpose HTML5 machinery website – 0527 Practical Collection pbootcms Template

A bilingual (Chinese/English) PbootCMS website template for mechanical equipment and general machinery applications, compatible with both PC and WAP devices. Its sophisticated, international design makes it ideal for foreign trade enterprises specializing in mechanical equipment to showcase their products. The multilingual support facilitates effective brand promotion for mechanical equipment companies targeting overseas markets. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 52