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 — Apre un nuovo file nella modalità specificata da flags e i permessi indicati in mode
Descrizione
$filename
, int $flags
[, int $mode
] )La funzione dio_open() apre un file e restituisce un nuovo descrittore di file per questo.
Elenco dei parametri
-
filename -
Il file aperto.
-
flags -
Il parametro
flagspuò contenere qualsiasi combinazione dei seguenti valori:-
O_CREAT- crea un file, se questo non esiste già. -
O_EXCL- se sono impostati siaO_CREATe siaO_EXCL, la funzione dio_open() fallisce se il file esiste. -
O_TRUNC- se il file esiste, ed è aperto in scrittura, il file verrà portato a lunghezza zero. -
O_APPEND- nelle operazioni di scrittura, scrive i dati alla fine del file. -
O_NONBLOCK- imposta la modalità non blocking.
-
-
mode -
Se
flagsvaleO_CREAT, allora il parametromodeimposta la modalità del file (permessi di creazione).-
O_RDONLY- apre il file per accessi in lettura. -
O_WRONLY- apre il file in scrittura. -
O_RDWR- apre il file sia in lettura sia in scrittura.
-
Valori restituiti
Restituisce un descrittore di file, oppure FALSE in caso di errore.
Esempi
Example #1 Apertura di un descrittore di file
<?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);
