PHP 8.3.4 Released!

dio_open

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

dio_open Apre un nuovo file nella modalità specificata da flags e i permessi indicati in mode

Descrizione

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

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 flags può contenere qualsiasi combinazione dei seguenti valori:

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

  • O_EXCL - se sono impostati sia O_CREAT e sia O_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 flags vale O_CREAT, allora il parametro mode imposta 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);
?>

Vedere anche:

  • dio_close() - Chiude il descrittore di file dato da 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
20 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