phpday 2027 - Call for Papers

dio_open

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

dio_open — Apre un file (creandolo se necessario) a un livello più basso di quello consentito dalle funzioni di input/output con stream della libreria C

Descrizione

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

dio_open() apre un file e restituisce un nuovo descrittore di file per esso.

Elenco dei parametri

filename

Il percorso del file da aprire.

flags

Il parametro flags è un valore combinato con OR bit a bit composto da flag della lista seguente. Questo valore deve includere uno tra O_RDONLY, O_WRONLY o O_RDWR. Inoltre, può includere qualsiasi combinazione degli altri flag di questa lista.

  • O_RDONLY - apre il file in lettura.

  • O_WRONLY - apre il file in scrittura.

  • O_RDWR - apre il file sia in lettura che in scrittura.

  • O_CREAT - crea il file, se non esiste già.

  • O_EXCL - se sono impostati sia O_CREAT che O_EXCL e il file esiste già, dio_open() fallirà.

  • O_TRUNC - se il file esiste ed è aperto in scrittura, viene troncato a lunghezza zero.

  • O_APPEND - le operazioni di scrittura scrivono i dati alla fine del file.

  • O_NONBLOCK - imposta la modalità non bloccante.

  • O_NOCTTY - impedisce al sistema operativo di assegnare il file aperto come terminale di controllo del processo quando si apre un file di dispositivo TTY.

mode

Se flags contiene O_CREAT, mode imposta i permessi del file (permessi di creazione). mode è necessario per il corretto funzionamento quando O_CREAT è specificato in flags, e viene ignorato altrimenti.

I permessi effettivi assegnati al file creato dipendono come di consueto dall'impostazione umask del processo.

Valori restituiti

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);
?>

Vedere anche:

  • dio_close() - Chiude il descrittore di file dato da fd

User Contributed Notes 3 notes

up
5
j at pureftpd dot org
21 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
3
Marius Karthaus
16 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
Anonymous
14 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
Adding new notes is disabled. Please see the links in the Found A Problem? section.
To Top