-
2021-05-04 09:23:31
这篇文章主要介绍了php中opendir函数用法,以实例形式详细讲述了opendir函数打开目录的用法及相关的注意事项,具有一定的参考借鉴价值,需要的朋友可以参考下
本文实例分析了php中opendir函数用法。分享给大家供大家参考。具体如下:
opendir语法:opendir(path,context)
目录,功能说明:打开目录句柄,opendir() 函数打开一个目录句柄,则该函数返回一个目录流,否则返回false.来看个opendir列出目录下所有文件实例,代码如下:
代码如下:$dirs ='./';//指定当前上当
if( is_dir( $dirs ) )
{
$hanld = opendir($dirs);
while (($file = readdir($hanld)) !== false)
{
echo "文件名: " . $file . "
";}
closedir($hanld);
}
else
{
echo '不是目录';
}
输出结果:
文件名:a
文件名:b
文件名:www.gxlcms.com
提示和注释:
注释:从 PHP 5.0.0 开始,path 参数支持 ftp:// URL wrapper
注释:在 PHP 4.3.0 中,path 参数可以是任何支持目录列表的 URL,不过在 PHP 4 中只有 file:// URL wrapper 支持此功能.
更多相关内容 -
PHP遍历目录函数opendir()、readdir()、closedir()、rewinddir()总结
2020-12-19 04:34:48取得一个目录下的文件和子目录,就需要用到opendir()函数、readdir()函数、closedir()函数和rewinddir()函数。 ①函数opendir() 函数opendir()用于打开指定目录,接受一个目录的路径及目录名作为参数,函数返回值为... -
php中opendir函数用法实例
2020-10-25 05:58:29主要介绍了php中opendir函数用法,以实例形式详细讲述了opendir函数打开目录的用法及相关的注意事项,具有一定的参考借鉴价值,需要的朋友可以参考下 -
php目录遍历函数opendir用法实例
2020-12-19 12:09:17opendir()函数的作用是:打开目录句柄,如果该函数成功运行,将返回一组目录流(一组目录字符串),如果失败将返回错误[error],你可以在函数的最前面加上“@”来隐藏错误. syntax语法:opendir(directory,context) ... -
管理文件和目录之opendir和closedir目录
2022-02-22 13:55:32管理文件和目录之opendir和closedir目录 -
C语言:opendir()函数—打开目录函数,readdir()函数—读取目录函数,所需的头文件~dirent.h
2017-11-28 20:30:29dirent.h:opendir()函数—打开目录函数,readdir()函数—读取目录函数,所需的头文件;dirent.h文件在压缩包的 include 目录中,examples目录中为一些使用示例。 -
php中目录操作opendir()、readdir()及scandir()用法示例
2021-01-02 17:06:51opendir(path,context)若成功,则该函数返回一个目录流,否则返回 false 以及一个 error。可以通过在函数名前加上 “@” 来隐藏 error 的输出。 readdir() 函数返回由 opendir() 打开的目录句柄中的条目。若成功,则... -
linux opendir和readdir的使用
2021-06-06 05:09:591 opendir#include #include DIR *opendir(const char *name);传入name路径,成功则返回非空DIR指针,否则返回NULL2 readdir#include struct dirent *readdir(DIR *dirp);int readdir_r(DIR *dirp, struct dirent *...1 opendir
#include
#include
DIR *opendir(const char *name);
传入name路径,成功则返回非空DIR指针,否则返回NULL
2 readdir
#include
struct dirent *readdir(DIR *dirp);
int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
readdir一般要配合opendir使用,readdir不是线程安全函数,代替他的有readdir_r。
readdir返回 struct dirent *指针,读完目录下所有文件时,返回NULL
如果系统支持readdir_r,建议用readdir_r , readdir_r成功返回0。
关于struct dirent结构体:
On Linux, the dirent structure is defined as follows:
struct dirent {
ino_t d_ino; /* inode number */
off_t d_off; /* not an offset; see NOTES */
unsigned short d_reclen; /* length of this record */
unsigned char d_type; /* type of file; not supported
by all filesystem types */
char d_name[256]; /* filename */
};
3 closedir
#include
#include
int closedir(DIR *dirp);
closedir配合opendir使用。
#include
#include
#include
#include
#include
#include
//DIR *opendir(const char *name);
#ifndef LOG_TRACE
#define LOG_TRACE printf
#define LOG_INFO(msg) \
do{ \
LOG_TRACE msg; \
LOG_TRACE("[%s %d] \n",__FUNCTION__,__LINE__);\
}while(0)
#define LOG_ERROR(err_info) \
do{ \
LOG_TRACE err_info; \
LOG_TRACE("[%s %d] \n",__FUNCTION__,__LINE__);\
}while(0)
#endif
extern int errno;
int open_dir_1(const char *pDirname)
{
DIR * dirp = NULL;
struct dirent * pDirent= NULL;
if((NULL == pDirname) ||
(0 == strlen(pDirname)))
{
LOG_ERROR(("param error"));
return -1;
}
dirp = opendir(pDirname);
if (NULL == dirp)
{
LOG_ERROR(("opendir %s failed! error_no: %s",pDirname , strerror(errno)));
return -1;
}
while (NULL != (pDirent = readdir(dirp)))
{
if (pDirent->d_type == DT_DIR )
{
LOG_ERROR(("dir [%s] ",pDirent->d_name));
}
else if(pDirent->d_type == DT_REG)
{
LOG_ERROR(("file [%s] ",pDirent->d_name));
}
}
closedir(dirp);
return 0;
}
int open_dir_2(const char *pDirname)
{
DIR * dirp = NULL;
struct dirent * pDirent= NULL;
struct dirent *pStResult = NULL;
if((NULL == pDirname) ||
(0 == strlen(pDirname)))
{
LOG_ERROR(("param error"));
return -1;
}
dirp = opendir(pDirname);
if (NULL == dirp)
{
LOG_ERROR(("opendir %s failed! error_no: %s",pDirname , strerror(errno)));
return -1;
}
pDirent = (struct dirent *)malloc(sizeof(struct dirent));
if(!pDirent)
{
LOG_ERROR(("pDirent error"));
closedir(dirp);
return -1;
}
while (( 0== readdir_r(dirp,pDirent,&pStResult))&&
(pStResult != NULL))
{
if (pDirent->d_type == DT_DIR )
{
LOG_ERROR(("dir [%s] ",pDirent->d_name));
}
else if(pDirent->d_type == DT_REG)
{
LOG_ERROR(("file [%s] ",pDirent->d_name));
}
}
closedir(dirp);
return 0;
}
int main()
{
LOG_ERROR(("***********"));
open_dir_1("/share/");
LOG_ERROR(("------------"));
open_dir_2("/share/");
LOG_ERROR(("-++--+++--"));
return 0;
}
运行结果:
./a.out [19@gcc test_opendir_readdir.c[C root@ubuntu:/share# root@ubuntu:/share# root@ubuntu:/share# gcc test_opendir_readdir.c /a.out ***********[main 113] file [zlib-1.2.8.tar.gz] [open_dir_1 54] file [1.tmp] [open_dir_1 54] file [log.c] [open_dir_1 54] file [list.c] [open_dir_1 54] dir [11] [open_dir_1 50] file [test.out] [open_dir_1 54] file [test_system_func.c] [open_dir_1 54] file [a.out] [open_dir_1 54] file [test_strncpy.c] [open_dir_1 54] file [test_proc_partitions.c] [open_dir_1 54] file [test.c] [open_dir_1 54] file [New0001.c] [open_dir_1 54] dir [curl-7.51.0] [open_dir_1 50] file [test_ftok.c] [open_dir_1 54] file [log.h] [open_dir_1 54] file [test_gettimeofday.c] [open_dir_1 54] file [csdn.c] [open_dir_1 54] file [client.c] [open_dir_1 54] file [1.txt] [open_dir_1 54] file [test_opendir_readdir.c] [open_dir_1 54] file [curl-7.51.0.tar.gz] [open_dir_1 54] file [2.txt] [open_dir_1 54] file [opendir.c] [open_dir_1 54] dir [zlib-1.2.8] [open_dir_1 50] file [123.rmvb] [open_dir_1 54] dir [ffmpeg_learn] [open_dir_1 50] file [test_sem.c] [open_dir_1 54] file [test_list.c] [open_dir_1 54] file [list.h] [open_dir_1 54] file [server] [open_dir_1 54] dir [ProFFmpeg] [open_dir_1 50] dir [ffmpeg-3.1.6] [open_dir_1 50] dir [yasm-1.3.0] [open_dir_1 50] file [New0003.c] [open_dir_1 54] dir [.] [open_dir_1 50] dir [..] [open_dir_1 50] file [ffmpeg-3.1.6.tar.gz] [open_dir_1 54] file [types.h] [open_dir_1 54] file [New0002.c] [open_dir_1 54] file [simple.out] [open_dir_1 54] file [server.c] [open_dir_1 54] dir [learn] [open_dir_1 50] file [simple_ffmpeg_player.c] [open_dir_1 54] file [client] [open_dir_1 54] dir [abc] [open_dir_1 50] file [yasm-1.3.0.tar.gz] [open_dir_1 54] file [output.yuv] [open_dir_1 54] file [mySDLFirst.out] [open_dir_1 54] ------------[main 115] file [zlib-1.2.8.tar.gz] [open_dir_2 100] file [1.tmp] [open_dir_2 100] file [log.c] [open_dir_2 100] file [list.c] [open_dir_2 100] dir [11] [open_dir_2 96] file [test.out] [open_dir_2 100] file [test_system_func.c] [open_dir_2 100] file [a.out] [open_dir_2 100] file [test_strncpy.c] [open_dir_2 100] file [test_proc_partitions.c] [open_dir_2 100] file [test.c] [open_dir_2 100] file [New0001.c] [open_dir_2 100] dir [curl-7.51.0] [open_dir_2 96] file [test_ftok.c] [open_dir_2 100] file [log.h] [open_dir_2 100] file [test_gettimeofday.c] [open_dir_2 100] file [csdn.c] [open_dir_2 100] file [client.c] [open_dir_2 100] file [1.txt] [open_dir_2 100] file [test_opendir_readdir.c] [open_dir_2 100] file [curl-7.51.0.tar.gz] [open_dir_2 100] file [2.txt] [open_dir_2 100] file [opendir.c] [open_dir_2 100] dir [zlib-1.2.8] [open_dir_2 96] file [123.rmvb] [open_dir_2 100] dir [ffmpeg_learn] [open_dir_2 96] file [test_sem.c] [open_dir_2 100] file [test_list.c] [open_dir_2 100] file [list.h] [open_dir_2 100] file [server] [open_dir_2 100] dir [ProFFmpeg] [open_dir_2 96] dir [ffmpeg-3.1.6] [open_dir_2 96] dir [yasm-1.3.0] [open_dir_2 96] file [New0003.c] [open_dir_2 100] dir [.] [open_dir_2 96] dir [..] [open_dir_2 96] file [ffmpeg-3.1.6.tar.gz] [open_dir_2 100] file [types.h] [open_dir_2 100] file [New0002.c] [open_dir_2 100] file [simple.out] [open_dir_2 100] file [server.c] [open_dir_2 100] dir [learn] [open_dir_2 96] file [simple_ffmpeg_player.c] [open_dir_2 100] file [client] [open_dir_2 100] dir [abc] [open_dir_2 96] file [yasm-1.3.0.tar.gz] [open_dir_2 100] file [output.yuv] [open_dir_2 100] file [mySDLFirst.out] [open_dir_2 100] -++--+++--[main 118] root@ubuntu:/share#
-
PHP opendir() 函数
2021-04-07 08:04:42打开一个目录,读取它的内容,然后...// Open a directory, and read its contentsif (is_dir($dir)){if ($dh = opendir($dir)){while (($file = readdir($dh)) !== false){echo "filename:" . $file . "";}closed...打开一个目录,读取它的内容,然后关闭:
$dir = "/images/";
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
echo "filename:" . $file . "
";}
closedir($dh);
}
}
?>
结果:
filename: cat.gif
filename: dog.gif
filename: horse.gif
定义和用法
opendir() 函数打开目录句柄。
语法
opendir(path,context);
参数
描述
path
必需。规定要打开的目录路径。
context
可选。规定目录句柄的环境。context 是可修改目录流的行为的一套选项。
技术细节
返回值:
成功则返回目录句柄资源。失败则返回 FALSE。如果路径不是合法目录,或者由于许可限制或文件系统错误导致的目录不能打开,则抛出 E_WARNING 级别的错误。您可以通过在函数名称前添加 '@' 来隐藏 opendir() 的错误输出。
PHP 版本:
4.0+
PHP 更新日志:
PHP 5.0:path 参数支持 ftp:// URL 封装协议。
opendir()函数
opendir() 函数打开一个目录句柄,可由 closedir(),readdir() 和 rewinddir() 使用. 若成功,则该函数返回一个目录流,否则返回 false 以及一个 error ...
php中opendir函数用法实例
这篇文章主要介绍了php中opendir函数用法,以实例形式详细讲述了opendir函数打开目录的用法及相关的注意事项,具有一定的参考借鉴价值,需要的朋友可以参考下 本文实例分析了php中opendi ...
opendir函数和readdir函数内涵及用法
工作中遇到奇怪的事,加载增量的时候加载不上.于是开始分析原因,log里边没有任何错误信息,只有加载完成的标志.增量的数据在目录里边是存在的,但是显示的目录大小却不是4096,而是17,不知道为什么.后 ...
Linux c —— opendir函数和readdir函数内涵及用法(转)
opendir函数 头文件:#include #include 函数:DIR *opendir(const char *na ...
opendir函数
#include #include DIR *dirptr = NULL; struct dirent *entry; dirpt ...
目录操作函数opendir、readdir和closedir
首先,明确一个类型DIR的含义: #include DIR A type representing a directory stream. DIR是在目录项格式 ...
利用目录函数(opendir,readdir,closedir)查找文件个数
如何知道一个目录下的所有文件个数呢?或许可以用tree来学(zhuang)习(bi)的同时知道文件个数.Linux系统io函数为我们提供了目录操作函数,其中有一个比较重要(实际上有三个,因为它们经常配 ...
PHP文件相关的操作函数——文件操作
1.文件的代开与关闭 1.1 fopen() 作用:该函数用于打开一个文件 具体使用访问:http://www.w3school.com.cn/php/func_filesystem_fopen.as ...
PHP文件相关的操作函数——目录操作
1.有关文件类型的函数 PHP是以UNIX的文件系统为模型的,因此在Windows系统中我们只能获得“file”.“dir”或者“unknown”三种文件类型.而在UNIX系统中,我们可以获得“blo ...
随机推荐
python之最强王者(4)——字符串
1.Python 中文编码 前面章节中我们已经学会了如何用 Python 输出 "Hello, World!",英文没有问题,但是如果你输出中文字符"你好,世界" ...
扩展progress_timer的计时精度
progress对外输出精度只有小数点后两位(这点可以运行上节程序进行验证),即精确到0.01秒. 我们使用模板技术仿造一个progress_timer编写一个新类:new_progress_time ...
It English 每日更新
unary operator 一元运算符 short circuit evaluation 短路经查询
POJ 1850 Code(组合数)
http://poj.org/problem?id=1850 题意 :给定字符串,系统是用字符串组成的,字符串是按字典序排的.编码系统有三条规则,1这些的单词的长度是由小到大的,2相同长度的按字母在字 ...
A Tour of Go Exercise: Loops and Functions
As a simple way to play with functions and loops, implement the square root function using Newton's ...
mysql基础示例
创建数据库.创建表等: //php中按天创建表 $sql = "create database if not exists ".$db_name; $date_time_array ...
Javascript 学习 笔记一
1.操作 HTML 元素 如需从 JavaScript 訪问某个 HTML 元素,您能够使用 document.getElementById(id) 方法. 请使用 &qu ...
vertx的HttpServer模块
Start HttpServer /** * 启动 HttpServer * multi instances 采用 synchronized防止线程安全问题 * addHandlers 方法是acto ...
毕业设计 之 五 PHP语法学习笔记
毕业设计 之 四 PHP语法学习笔记 作者:20135216 平台:windows10 软件:XAMPP,DreamWeaver 说明:该笔记是对网站编程语言的详细学习 一.PHP基础 0. 关于环境 ...
#js window.open() 父窗口与子窗口的互相调用【转】
未完整版 javascript 父窗口与子窗口的互相调用 a.html 父页面
-
opendir/readdir/closedir函数
2022-03-14 11:19:51实现一个简易的ls -R命令,遍历目录下所有内容。opendir/readdir/closedir和stat函数。opendi(3)/readdir(3)/closedir(3)
用于遍历目录数据块中的记录。opendir打开一个目录,返回一个DIR *指针代表这个目录,它是一个类似FILE *指针的句柄,closedir用于关闭这个句柄,把DIR *指针传给readdir读取目录数据块中的记录,每次返回一个指向struct dirent的指针,反复读就可以遍历所有记录,所有记录遍历完之后readdir返回NULL。strcut dirent的定义如下:
struct dirent{
ino_t d_ion; /*inode number*/
off_t d_off; /*offset to the next dirent*/
unsigned short d_reclen; /*length of this record*/
unsigned char d_type; /*type of file*/
char d_name[256]; /*filename*/
};
实现一个简易的ls命令,代码演示如下:
#include "./common/head.h" /*功能: *实现一个简易的ls -R功能,将目录里的内容,遍历打印出来。 */ //用于遍历打印目录中的内容 void printDir(char * dirname){ char pathname[1024]; DIR *dir; struct dirent *dp; struct stat st; if(!(dir = opendir(dirname))){ perror("opendir"); exit(1); } while(dp = readdir(dir)){ if( !strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..") ) continue; //当目录是.或..时,读取下一条记录,避免段错误 sprintf(pathname, "%s/%s", dirname, dp->d_name); //字符串拼接 if( stat(pathname, &st) < 0 ){ perror("stat"); exit(1); } if(S_ISDIR(st.st_mode)){ //如果是一个目录遍历调用它自己 printDir(pathname); } //运行到这里,代表不是目录,打印内容 printf("%s\t", dp->d_name); putchar(10); closedir(dir); } } int main(int argc, char *argv[]) { if(argc != 2){ printf("usage:cmd path\n"); return 1; } DIR *dir; if( !(dir = opendir(argv[1])) ){ //opendir打开失败返回NULL perror("opendir"); exit(1); } printDir(argv[1]); return 0; }
-
PHP opendir()用法及代码示例
2021-04-12 18:15:30要打开的目录的路径作为参数发送到opendir()函数,如果成功,则返回目录句柄资源;如果失败,则返回FALSE。opendir()函数用于打开目录句柄,以便随后与其他目录函数(例如closedir(),readdir()和rewinddir())一起... -
查看linux文件目录数 :opendir
2022-01-26 21:52:00dir = opendir(root); if(dir == NULL){ perror("opendir"); exit(1); } //foreach while((ptr = readdir(dir)) != NULL){ // . .. if(strcmp(ptr->d_name,".")==0||strcmp(ptr->d_name,"..")==0){ continue; } //... -
php opendir()函数讲解及遍历目录实例
2021-04-07 08:04:38opendir()函数介绍opendir() 函数打开目录句柄。成功则返回目录句柄资源。失败则返回 FALSE。如果路径不是合法目录,或者由于许可限制或文件系统错误导致的目录不能打开,则抛出 E_WARNING 级别的错误。您可以通过在... -
opendir函数_PHP opendir()函数与示例
2020-07-26 02:11:34opendir函数 PHP opendir()函数 ...The full form of opendir is "Open Directory", the function opendir() is used to open a directory. opendir的完整形式为“ Open Directory” , 函数opendir()用于打开目录... -
opendir和readdir
2019-12-17 22:42:57对于opendir的话,看完网课教程后面,我再试着根据仅有的记忆,自己通过man手册去写一个简单的测试代码。 SYNOPSIS #include #include DIR *opendir(const char *name); 这里就是man手册里面的解释... -
php opendir()列出目录下所有文件的实例代码
2020-12-18 23:57:40php opendir()函数用于打开目录,通常与readdir()和closedir()函数一起用来读取目录下所有文件(即遍历目录),本文章向大家介绍php使用opendir()函数列出目录下所有文件的实例。 实例一: 使用opendir()列出目录下... -
C语言--目录读取文件,opendir函数和readdir函数
2021-11-04 13:18:331,opendir 和 readdir函数 DIR *opendir(const char *name); 返回值:The opendir() and fdopendir() functions return a pointer to the directory stream. 解释:成功返回指向目录流的指针(DIR),失败 返回... -
php之opendir()函数的用法
2021-04-12 18:16:20PHP opendir()用来打开一个目录或文件夹,要打开的目录必须实体...opendir()常见于和closedir()、readdir() 和 rewinddir()配合使用。PHP opendir()若执行成功返回true,失败则返回false。opendir()函数的范例代码... -
linux 下 opendir 和 readdir 函数的应用
2021-05-16 08:18:12#include #include #include #include #include #include void printall(const char *path, int max){DIR *dir = opendir(path); //产生DIR的指针if (dir == NULL){return ;}struct dirent *dirent; //系统默认含有... -
Linux目录操作函数:opendir/readdir/closedir
2021-01-27 05:16:42DIR*opendir(constchar*path);structdirent*readdir(DIR*dir_handle);intclosedir(DIR*dir_handle);1.opendir头文件:#include#include函数原型:DIR*opendir(constchar*path);功能:打开一个目录,在失败的时候返回一... -
opendir
2014-12-04 16:15:06相关函数:open, readdir, closedir, rewinddir, seekdir, telldir, scandir 头文件:#include #include ...函数说明:opendir()用来打开参数name 指定的目录, 并返回DIR*形态的目录流, 和open()类似, 接 -
OpenDir Protocol-开源
2021-04-24 01:58:59Webserver mod,用于查看打开的目录。 -
opendir、readdir以及使用
2021-01-15 13:45:16opendir、readdir以及使用 opendir,打开一个目录。 函数原型:DIR *opendir(const char *name) DIR *fopendir(int fd) DIR是一个结构指针,是一个内部结构,保存所打开的目录信息。函数出错返回NULL ... -
opendir()和readdir()函数
2021-07-07 16:53:371. opendir() 头文件 #include<sys/types.h> #include<dirent.h> 函数原型 DIR* opendir (const char * path ); (获取path子目录下的所由文件和目录的列表,如果path是个文件则返回值为NULL) ... -
嵌入式学习之linux系统编程---8 目录IO之opendir和closedir函数
2022-03-24 11:09:17opendir函数原型 #include<sys/types.h> #include<dirent.h> DIR *opendir(const char*name); "DIR opendir(const charname);"代表的是:返回值是目录流指针,如果失败的话返回值是null,成功返回目录... -
opendir()函数:打开目录函数
2021-09-01 19:25:25opendir()用来打开参数name 指定的目录, 并返回DIR*形态的目录流, 和open()类似, 接下来对目录的读取和搜索都要使用此返回值. 4,返回值: 成功则返回DIR* 型态的目录流, 打开失败则返回NULL. 5,错误代码: 1、... -
Linux下opendir、readdir使用小结
2021-07-07 16:44:111.opendir小结 #include<sys/types.h> #include<dirent.h> DIR *opendir(const cahr *path); 函数功能:获取path目录下所有文件和目录的列表,如果path是个文件或者失败则返回NULL 返回值(DIR) ...