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_WRONLYO_RDWR 中的一个。此外,可以包含此列表中其它标志的任意组合。

  • O_RDONLY - 打开文件读访问。

  • O_WRONLY - 打开文件写访问。

  • O_RDWR - 打开文件读写。

  • O_CREAT - 如果文件不存在,创建文件。

  • O_EXCL - 如果同时设置了 O_CREATO_EXCL,并且文件已存在,dio_open() 将执行失败。

  • O_TRUNC - 如果文件存在且已打开写访问,将会被截断到 0 长度。

  • O_APPEND - 写操作在文件末尾写数据。

  • O_NONBLOCK - 设置非阻塞模式。

  • O_NOCTTY - 防止操作系统在打开 TTY 设备文件时将文件指定为进程的控制终端。

mode

如果 flags 包含 O_CREATmode 将设置文件权限(创建权限)。当 flags 指定了 O_CREAT 标志时,需要 mode 才能正确操作,否则将会被忽略。

分配给创建文件的实际权限照例受到进程的 umask 设置的影响。

返回值

文件描述符,错误时是 false

示例

示例 #1 打开文件描述符

<?php

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

dio_close($fd);
?>

参见

add a note add a note

User Contributed Notes 4 notes

up
4
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
2
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
1
Anonymous
12 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
-9
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