Hongmu Notes
Home Language Notes What is the code to retrieve files from a folder in PHP?
Language Notes PHP

What is the code to retrieve files from a folder in PHP?

What is the code to retrieve files from a folder in PHP?

To retrieve all files within a folder using PHP, you can use the `glob()` or `scandir()` functions. Below are example codes for both methods:

  1. glob()function

The `glob()` function accepts a single parameter and returns an array containing all files and directories that match the specified pattern. To retrieve all files in a directory, you can use the following code:

$files = glob('/path/to/folder/*');
foreach ($files as $file) {
    echo $file . '<br>';
}

In the above code, '/path/to/folder/' is the path to the directory you wish to search within. The wildcard '*' represents all files and subdirectories that you want to match. Then, we use a foreach loop to iterate over this array and output each file name.

  1. `scandir()` function

The `scandir()` function returns an array containing all files and folders in a specified directory. You can use the following example code to retrieve all files from a directory:

$files = scandir('/path/to/folder/');
foreach ($files as $file) {
    if ($file != '.' && $file != '..') { // 忽略“.”和“..”目录
        echo $file . '<br>';
    }
}

In the above code, '/path/to/folder/' is the path to the directory you wish to search through. We use a foreach loop to iterate over this array and an if statement to ignore the filenames of the current directory and its parent directory ('.' and '..', respectively). Then, we output all remaining filenames.

In summary, using either the `glob()` or `scandir()` function to retrieve all files in a directory is relatively straightforward, and both methods can meet this requirement. Please choose the approach that best suits your specific use case.

微信赞赏

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

(Adaptive Mobile Version) Travel Guide Website Template – Article/Blog Website Source Code – With Comment Feature – 1142

(Adaptive Mobile Version) Travel Guide Website Template – Article/Blog Website Source Code – With Comment Feature – 1142 Practical Collection pbootcms Template

A PbootCMS website template designed for travel guides and blog posts, compatible with both PC and WAP devices, and featuring a comment section. Its fresh, travel-themed design makes it ideal for travel enthusiasts to share their travel stories, guides, and photography. The comment feature enhances user interaction and helps foster a community atmosphere. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 64
Insert related articles in the middle of imperial cms articles (article page optimization, the effect is super good)

Insert related articles in the middle of imperial cms articles (article page optimization, the effect is super good) Practical Collection empire plugin

Those who understand will know – this feature allows you to aggregate related articles. By displaying these articles in the middle of the main content, it not only enhances the user experience but also achieves effective optimization. I've noticed that many high-authority websites are already using this feature, so I spent several hours researching it and wrote this tutorial! The benefit for website optimization is significant – I'm sure even seasoned SEO experts will agree! This feature was suggested to me by an experienced website optimization expert; I hope it will be helpful to all webmasters! Whether you run a content aggregation site or...
👁 474
Responsive Eco-Agricultural Farm Website Template 0727

Responsive Eco-Agricultural Farm Website Template 0727 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for eco-agricultural farms, featuring a fresh and natural design style perfect for showcasing eco-agricultural products, cultivation sites, organic certifications, and brand stories. It helps eco-agricultural businesses showcase their products online and attract health-conscious consumers. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | Eyou...
👁 30
(Adaptive mobile version) Heavy Industry Machinery Website – PBootCMS Template; Excavator & Bulldozer Website Source Code Download – 0409

(Adaptive mobile version) Heavy Industry Machinery Website – PBootCMS Template; Excavator & Bulldozer Website Source Code Download – 0409 Practical Collection pbootcms Template

This adaptive mobile-friendly PbootCMS website template is designed for heavy-duty industrial machinery, excavators, and bulldozers. Its bold yellow design is ideal for showcasing large-scale construction machinery products and project cases. It helps heavy machinery enterprises effectively demonstrate their product capabilities online and attract clients in the infrastructure and mining sectors. Template Overview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 54
(PC+WAP) Cable Coil Website Template – Download Plastic Cable Coil Website Source Code – 1143

(PC+WAP) Cable Coil Website Template – Download Plastic Cable Coil Website Source Code – 1143 Practical Collection pbootcms Template

A cable reel and plastic reel website template (PbootCMS), compatible with both PC and WAP devices. The professional industrial design is ideal for showcasing cable reel products, plastic products, and industrial applications. It enables cable reel manufacturers to display their products online and attract clients in the power and telecommunications sectors. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 46
Acupuncture and moxibustion acupoint markings on the human body

Acupuncture and moxibustion acupoint markings on the human body Hobbies Memo notes

Ren meridian Ren meridian acupoint song Ren meridian thirty-eight starts from the perineum, the pole in the curved bone is Guan Yuanrui, Shimen qi and sea yin still intersect, and the water from Shenque flows down to the epigastrium. The middle and upper buildings of the building are connected, with a huge dove tail covering the bones, a Mu Yu Hall in the middle of the atrium, and a purple palace canopy at night. Tiantu's throat is Lianquan, and there are twenty-four points under the lip. This sutra does not take the combination of Jing Ying and Shu. The pulse starts below the middle pole, above the hairline, along the Guanyuan in the abdomen, to the throat. It belongs to the sea of ​​yin veins. It flows around the divisions of yin as human veins, just like water...
👁 672