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.
dio_open
(PHP 4 >= 4.2.0, PHP 5 <= 5.0.5)
dio_open — Öffnet eine Datei mit den angegebenen Rechten
Beschreibung
$filename
, int $flags
[, int $mode = 0
] )dio_open() öffnet eine Datei und liefert einen Dateideskriptor zurück.
Parameter-Liste
-
filename -
Die zu öffnende Datei.
-
flags -
Der Parameter
flagskann eine Kombination der folgenden Flags enthalten:-
O_CREAT - erzeugt die Datei, falls diese nicht existiert
-
O_EXCL - in Kombination mit
O_CREATwird ein Fehler (-1) zurückgegeben, wenn die Datei bereits existiert -
O_TRUNC - falls die Datei bereits existiert, und sie für Schreibzugriff geöffnet wird, wird diese auf die Grösse 0 zurückgesetzt
-
O_APPEND - setzt den Zeiger für Schreiboperationen an das Ende der Datei
-
O_NONBLOCK - Setzt den Modus auf "nonblocking"
-
-
mode -
Wenn
flagsdas FlagO_CREATenthältmodedie Dateizugriffsrechte.-
O_RDONLY - öffnet die Datei für Lesezugriff
-
O_WRONLY - öffnet die Datei für Schreibzugriff
-
O_RDWR - öffnet die Datei für Schreib- und Lesezugriff
-
Rückgabewerte
Ein Dateideskriptor oder FALSE bei Fehlern.
Beispiele
Beispiel #1 Öffnen eines Dateideskriptors
<?php
$fd = dio_open('/dev/ttyS0', O_RDWR | O_NOCTTY | O_NONBLOCK);
dio_close($fd);
?>
"The prominent reason" to use direct I/O is when your application provides its own cache feature, so you won't do double caching
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.
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);
