opendir

opendir函式

基本介紹

  • 中文名:opendir
  • 外文名:opendir
  • 屬性:函式
  • 語言:PHP、C++
  • 平台:桌面
php中的opendir函式,Linux C中的opendir,頭檔案,函式原型,功能,Perl中的opendir,函式原型:,功能說明:,示例:,

php中的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

Linux C中的opendir

頭檔案

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

函式原型

DIR* opendir (const char * path );

功能

打開一個目錄,在失敗的時候返回一個空的指針。
使用實例:
#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;
}

Perl中的opendir

函式原型:

opendir DIRHANDLE,EXPR;

功能說明:

打開目錄句柄。
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.

示例:

#!/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);

相關詞條

熱門詞條

聯絡我們