複製鏈接
請複製以下鏈接發送給好友

opendir

鎖定
opendir,php中的opendir函數
中文名
opendir
外文名
opendir
屬    性
函數
語    種
PHP、C++
平    台
桌面

opendir基本介紹

The opendir() function opens a directory handle to be used by the closedir(), readdir(), and rewinddir() functions.
opendir()函數的作用是:打開目錄句柄。
This function returns a directory stream on success and FALSE and an error on failure. You can hide the error output by adding an '@' in front of the function name.
如果該函數成功運行,將返回一組目錄流(一組目錄字符串),如果失敗將返回錯誤[error]。你可以在函數的最前面加上“@”來隱藏錯誤。
Syntax
語法
opendir(directory,context)
Parameter
參數 Description
描述
directory Required. Specifies the directory to stream
必要參數。指定目錄對象
context Optional. Specifies the context of the directory handle. Context is a set of options that can modify the behavior of a stream
可選參數。指定需要處理的目錄對象的context。這個context包括了一組選項,它可以對文本流的顯示方式進行改變
--------------------------------------------------------------------------------
Tips and Notes
注意點
Note: From PHP 5 the directory parameter supports the ftp:// URL wrapper.
注意:PHP 5.0以上版本中,目錄參數支持ftp://URL。
--------------------------------------------------------------------------------
Example 1
案例1
<?php
//Open images directory
$dir = opendir("images");//List files in images directory
while (($file = readdir($dir)) !== false)
{
echo "filename: " . $file . "
";
}
closedir($dir);
?>
The output of the code above could be:
上述代碼將輸出下面的結果:
filename: .
filename: ..
filename: cat.gif
filename: dog.gif
filename: food
filename: horse.gif
--------------------------------------------------------------------------------
Example 2
案例2
This example hides the error if opendir() fails:
這個例子展示了:如果dir()函數運行失敗,自動將錯誤信息隱藏。具體如下:
<?php
//Open images directory
$dir = @ opendir("images");//List files in images directory
while (($file = readdir($dir)) !== false)
{
echo "filename: " . $file . "
";
}
closedir($dir);
?>
The output of the code above could be:
上述代碼將輸出下面的結果:
filename: .
filename: ..
filename: cat.gif
filename: dog.gif
filename: food
filename: horse.gif

opendirLinux C中的opendir

opendir頭文件

#include<sys/types.h>
#include<dirent.h>

opendir函數原型

DIR* opendir (const char * path );

opendir功能

打開一個目錄,在失敗的時候返回一個空的指針。
使用實例:
#include <stdio.h>
#include <dirent.h>
int main(int argc,char** argv)
{
DIR *dirptr = NULL;
struct dirent *entry;
if(argc<2)
{
printf("the command need a dirname\n");
return 1;
}
if(argc>2)
{
printf("the program can only deal with one dir at once\n");
return 1;
}
if((dirptr = opendir(argv[1])) == NULL)
{
printf("open dir error !\n");
return 1;
}
else
{
while (entry = readdir(dirptr))
{
printf("%s\n", entry->d_name);
}
closedir(dirptr);
}
return 0;
}

opendirPerl中的opendir

opendir函數原型

opendir DIRHANDLE,EXPR;

opendir功能説明

打開目錄句柄。
DIRHANDLE:打開的目錄句柄,供後續處理使用;
EXPR:指定的待打開的目錄路徑。
Perl Manualpage :
Opens a directory named EXPR for processing by readdir, telldir, seekdir, rewinddir, and closedir. Returns true if successful. DIRHANDLE may be an expression whose value can be used as an indirect dirhandle, usually the real dirhandle name. If DIRHANDLE is an undefined scalar variable (or array or hash element), the variable is assigned a reference to a new anonymous dirhandle; that is, it's autovivified. DIRHANDLEs have their own namespace separate from FILEHANDLEs. [1] 

opendir示例

#!/usr/bin/perl -w
$dirname = "/tmp"; #指定一個目錄
opendir ( DIR, $dirname ) || die "Error in opening dir $dirname\n";
while(($filename = readdir(DIR))){
print("$filename\n"); #循環輸出該目錄下的文件。
### do something ###
}
closedir(DIR); [2] 
參考資料