search
Homephp教程php手册Usage examples of opendir function in php

This article mainly introduces the usage of the opendir function in php. It details the usage of the opendir function to open a directory and related precautions in the form of examples. It has certain reference value. Friends in need can refer to it

This article analyzes the usage of opendir function in php with examples. Share it with everyone for your reference. The details are as follows:

opendir syntax: opendir(path,context)

Directory, function description: open a directory handle, the opendir() function opens a directory handle, then the function returns a directory stream, otherwise it returns false. Let's look at an example of opendir listing all files in the directory. The code is as follows:

<span style="color: #800080;">$dirs</span> ='./';<span style="color: #008000;">//</span><span style="color: #008000;">指定当前上当 </span>
<span style="color: #0000ff;">if</span>( <span style="color: #008080;">is_dir</span>( <span style="color: #800080;">$dirs</span><span style="color: #000000;"> ) ) 
{ 
 </span><span style="color: #800080;">$hanld</span> = <span style="color: #008080;">opendir</span>(<span style="color: #800080;">$dirs</span><span style="color: #000000;">); 
 
 </span><span style="color: #0000ff;">while</span> ((<span style="color: #800080;">$file</span> = <span style="color: #008080;">readdir</span>(<span style="color: #800080;">$hanld</span>)) !== <span style="color: #0000ff;">false</span><span style="color: #000000;">) 
 { 
   </span><span style="color: #0000ff;">echo</span> "文件名: " . <span style="color: #800080;">$file</span> . "<br>"<span style="color: #000000;">; 
 } 
 </span><span style="color: #008080;">closedir</span>(<span style="color: #800080;">$hanld</span><span style="color: #000000;">); 
} 
</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> 
{ 
 </span><span style="color: #0000ff;">echo</span> '不是目录'<span style="color: #000000;">; 
}</span>

Output result:
File name: a
File name: b
File name: www.manongjc.com

Tips and Notes:

Note: Starting from PHP 5.0.0, the path parameter supports ftp:// URL wrapper

Note: In PHP 4.3.0, the path parameter can be any URL that supports directory listing, but in PHP 4 only the file:// URL wrapper supports this feature.

I hope this article will be helpful to everyone’s PHP programming design.

Related reading:

http://www.manongjc.com/article/1495.html

http://www.manongjc.com/article/1496.html

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use