CakeFest 2024: The Official CakePHP Conference

dio_open

(PHP 4 >= 4.2.0, PHP 5 < 5.1.0)

dio_open C ライブラリの入出力ストリーム関数が許すよりも低レベルでファイルをオープンする (必要ならファイルを作成する)

説明

dio_open(string $filename, int $flags, int $mode = 0): resource

dio_open() は、ファイルをオープンして そのファイル記述子を返します。

パラメータ

filename

オープンするファイルのパス。

flags

flags パラメータには、 以下のフラグの組み合わせを指定します。 この値は、O_RDONLYO_WRONLY、 あるいは O_RDWR のいずれかでなければなりません。 さらに、このリストからその他の値を組み合わせることもできます。

  • O_RDONLY - 読み込み専用でファイルをオープンします。

  • O_WRONLY - 書き込み専用でファイルをオープンします。

  • O_RDWR - 読み書き両用でファイルをオープンします。

  • O_CREAT - ファイルが存在しなければ 新しいファイルを作成します。

  • O_EXCL - O_CREAT および O_EXCL をともに指定すると、 すでにファイルが存在する場合に dio_open() が失敗します。

  • O_TRUNC - すでにファイルが存在し、書き込み アクセス用にオープンされている場合、ファイルの長さをゼロに切り詰めます。

  • O_APPEND - 書き込み時は、ファイルの終端に 追記します。

  • O_NONBLOCK - 非ブロッキングモードを指定します。

  • O_NOCTTY - TTY デバイスファイルをオープンするときに、 オープンしたファイルをプロセスが自動的に制御端末としないようにします。

mode

flagsO_CREAT を含む場合に、mode でファイルのモード (作成許可) を指定します。O_CREATflags に指定されている場合には mode が必須となり、それ以外の場合は無視されます。

ファイルを作成したときに実際に設定されるモードは、プロセスの umask 設定によって変わります。

戻り値

ファイル記述子を返します。エラー時には false を返します。

例1 ファイル記述子をオープンする

<?php

$fd
= dio_open('/dev/ttyS0', O_RDWR | O_NOCTTY | O_NONBLOCK);

dio_close($fd);
?>

参考

  • dio_close() - fd で指定したファイル記述子を閉じる

add a note

User Contributed Notes 4 notes

up
4
j at pureftpd dot org
19 years ago
Please note that dio_open()/dio_write()/dio_close() is *faster* than fopen()/fwrite()/fclose() for files.

fwrite() has to manage a 8k buffer, while dio_write() just issue a single write(). The end result is less system calls and less memory access.

Also, giving the full size to write() as with dio_write() let filesystems properly use preallocation in order to avoid fragmentation.
up
2
Marius Karthaus
14 years ago
One of the prominent reasons to use direct IO, is for it's ability to do actual direct IO, bypassing the operating system cache and getting the data from the disk directly.
The flag to do that (O_DIRECT) is missing from the documentation above. Maybe for good reasons, because this type of IO only works on blockdevices, not on files, and should only be used if you are **really** sure what you are doing.
up
0
Anonymous
11 years ago
"The prominent reason" to use direct I/O is when your application provides its own cache feature, so you won't do double caching
up
-12
alla at cyber dot com dot au
21 years ago
To specify a combination of flags you OR them together.
This was the only way I could get it to append:

$fd = dio_open($file, O_WRONLY | O_APPEND);
To Top