ホームページ  >  記事  >  バックエンド開発  >  PHPディレクトリ

PHPディレクトリ

WBOY
WBOYオリジナル
2024-08-29 13:09:091058ブラウズ

PHP ディレクトリ関数は、その名前が示すように、詳細の取得、変更、さまざまなファイル システム ディレクトリとその特定のコンテンツに関する情報のフェッチに使用される一連の関数です。現在の作業ディレクトリの作成、削除、変更、ディレクトリ内に存在するファイルの一覧表示など、ディレクトリに対して多くの操作を実行できます。これらの関数は PHP コアの一部として提供されるため、個別にインストールする必要はありません。ただし、chroot() 関数を有効にするには、–enable-chroot-func オプションを設定する必要があります。

広告 このカテゴリーの人気コース PHP 開発者 - 専門分野 | 8コースシリーズ | 3 つの模擬テスト

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

PHPディレクトリの機能

以下に示すように、基本的な PHP ディレクトリ関数をいくつか見てみましょう:

1.新しいディレクトリを作成します

mkdir() 関数を使用して、PHP プログラミング スクリプトで新しいディレクトリを作成します。

構文:

mkdir($dir_path,$mode,$recursive_flag,$context);

どこ、

  • $dir_path は、指定された新しいディレクトリが作成される相対パスまたは絶対パスです。
  • $mode は、新しく作成されたディレクトリにアクセスできるレベルを決定する 8 進数値を受け取るパラメータです。
  • $recursive は、true または false の 2 つの値を持つフラグ タイプのフィールドで、ネストされたディレクトリを作成できるかどうかを指定できます。
  • $context は、特定のプロトコルを指定するストリームを持つなど、PHP unlink() で使用するものと似ています。これも、実行が正常に完了した場合は true、それ以外の場合は false となるブール値のみを返します。

例:

<?php
mkdir("/articles/");
echo("Directory created");
?>

出力:

PHPディレクトリ

これは、必要なパスにディレクトリを作成する方法を示す基本的な例です。パスに十分な権限があることを確認してください。そうでない場合、「権限が拒否されました」エラーがスローされます。

2.ディレクトリの内容を一覧表示する

ディレクトリリンクを開くためとそれを読み取るために、それぞれ opendir() と readdir() を使用します。ステップ 1 ではディレクトリを開き、ステップ 2 ではディレクトリを読み取ります。

ステップ 1: ディレクトリ リンクを開くには、opendir() がこのステップの実行に使用する関数です。以下に指定されているように、2 つの入力引数が必要です。

構文:

opendir($dir_path,$context);
  • $dir_path は、開く必要があるディレクトリのパスです。
  • $context は、コンテキスト ストリームが存在するかどうかを指定できるオプションのパラメーターです。

これは、出力としてリソース データ値を返します。提供されるこのリソース ID は、以降の処理ステップで使用されます。そうでない場合、リソース ID が無効であるため、エラーが発生します。

ステップ 2: ディレクトリの内容を読み取るには、readdir() がこの目的に使用される関数であり、ディレクトリがディレクトリの最後に到達するまで再帰的に呼び出す必要があります。ハンドル。

:

<?php
$direct = "/files/";
if (is_dir($direct)){
if ($td = opendir($direct)){
while (($file = readdir($td)) !== false){
echo "filename:" . $file . "<br>";
}
closedir($td);
}
}
?>

出力:

PHPディレクトリ

この例では、まず、読み取る必要があるディレクトリ パスを宣言しています。 if ステートメントでディレクトリが存在するかどうかをチェックし、ディレクトリの内容を開いて読み取りを進めます。出力には、ディレクトリ内に存在するファイル名が表示されます。

3.ディレクトリを閉じるには

ディレクトリの内容を読み取った後にディレクトリを閉じるために、closedir() 関数を使用します。

構文:

$dir_handle = opendir($dir_path);
...
...
closedir($dir_handle);

例:

<?php
$dir = "/file1";
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
$direc = readdir($dh);
echo("File present inside directory are:" .direc);
closedir($dh);
echo("Closed directory");
}
}
?>

出力:

PHPディレクトリ

この例では、最初にディレクトリのパスを宣言しています。次に、if 条件ステートメントを使用して、パスが有効かどうかを確認し、有効であれば、ディレクトリを開いてその変数を読み取ってから閉じます。したがって、ディレクトリを開いてから閉じるまでの間に、あらゆる操作を実行できます。

4.現在のディレクトリを変更するには

関数 chdir() を使用して、関数が指す現在の作業ディレクトリを変更します。

構文:

chdir(directory)

必要なパラメータは、現在の作業ディレクトリが指すディレクトリを 1 つだけ必要とします。ディレクトリの変更が成功した場合は true を返し、ディレクトリの変更に失敗した場合は false を返します。

例:

<?php
// Get current directory
echo getcwd()."\n";
// Change directory
chdir("/workspace/test");
// Get current directory
echo getcwd();
?>

出力:

PHPディレクトリ

In this example, we are first printing the present working directory. Then we are changing the same using chdir function to “test” directory and printing the same on the output. Hence make sure the entire path we are giving here exists.

5. To Change the Directory Path of Root

We use the function chroot() for changing the root directory of the ongoing process to the directory path we pass as an argument in this function. Also, the present working directory path will be changed to “/”. To perform this function one needs root permission/privileges.

Syntax:

chroot(directory)

Example:

<?php
// Changing root directory path
chroot("/change/path/dir/");
// Displaying present directory
echo getcwd();
?>

Output:

PHPディレクトリ

In this example, we are first using the chroot function to change the path of the root directory. Next, we are displaying the present working directory which will be now changed to home path.

6. To Reset the Directory Handle

For this purpose, we are using rewinddir() function which can reset the directory handle initially created by opendir() function.

Syntax:

rewinddir(directory)

It accepts only the directory path as its input argument which is used to tell the directory handle resource path which was opened with opendir() previously. This is an optional parameter which if not specified then the previous link used by the opendir() will be considered.

Example:

<?php
$direc = "/file/";
// To open the directory and read its contents
if (is_dir($direc)){
if ($place = opendir($direc)){
// List files in images directory
while (($file = readdir($place)) !== false){
echo "filename:" . $file . "\n";
}
rewinddir();
echo("Using the function rewinddir\n");
// List files again
while (($file = readdir($place)) !== false){
echo "filename:" . $file . "\n";
}
closedir($place);
echo("Closed directory");
}
}
?>

Output:

PHPディレクトリ

In this example first, we are specifying the directory path and if statement we are using to verify if the directory path is present or not. If the directory is present then we are opening and reading the contents of the file and printing the same. Now the file handler will stop printing since it reached the end of file pointer. When we use the rewinddir() function it resets the file handler and hence when we print the directory contents it prints the same output again.

Conclusion

We have gone through some of the basic and important PHP directory functions commonly used in this article. We also noticed that a few of these functions are dependant on each other. For example, we cannot use readdir() without using opendir(). Few other functions which are used are dir(), scandir() and getcwd().

以上がPHPディレクトリの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。