PHP - Direct I/O dio_write() Function



The PHP Direct I/O dio_write() function is used to write data to a file descriptor with optional truncation of length. It is a part of the Direct I/O functions, which allow low-level file access.

Syntax

Below is the syntax of the PHP Direct I/O dio_write() function −

int dio_write ( resource $fd , string $data [, int $len ] )

Parameters

Here are the parameters of the dio_write() function −

  • $fd: It is the file descriptor returned by dio_open().

  • $data: It is the data to write.

  • $len: It is the number of bytes to write. If not given then the complete data string will be written.

Return Value

The dio_write() function returns the number of bytes written, or FALSE on failure.

PHP Version

First introduced in core PHP 4.2.0, the dio_write() function continues to function easily in PHP 5.1.0.

Example 1

Here is the first basic example of the PHP Direct I/O dio_write() function to to write a simple string to a file.

<?php
   // Open a file for writing
   $fd = dio_open('/PHP/PhpProjects/myfile.txt', O_WRONLY | O_CREAT);
   
   // Write data to the file
   $data = "Hello, World!";
   
   $bytes_written = dio_write($fd, $data);
   
   // Close the file
   dio_close($fd);
   
   echo "Bytes written: " . $bytes_written;
?>

Output

After running the above program, it generates the following output −

Bytes written: 13

Example 2

In this PHP example code we will use the dio_write() function and write only a portion of a string in the file.

<?php
   // Open a file for writing
   $fd = dio_open('/PHP/PhpProjects/myfile.txt', O_WRONLY | O_CREAT);
   
   // Open a file for writing
   $fd = dio_open('example.txt', O_WRONLY | O_CREAT);
   
   // Write only the first 5 bytes of the string
   $data = "Hello, Tutorialspoint!";
   $bytes_written = dio_write($fd, $data, 5);
   
   // Close the file
   dio_close($fd);
   
   echo "Bytes written: " . $bytes_written;
?> 

Output

The above code will result something like this −

Bytes written: 5

Example 3

Now the below code uses dio_write() to append data to the given existing file.

<?php
   // Open a file for appending
   $fd = dio_open('/PHP/PhpProjects/myfile.txt', O_WRONLY | O_APPEND);
   
   // Append data to the file
   $data = "\nThis is additional text.";
   $bytes_written = dio_write($fd, $data);
   
   // Close the file
   dio_close($fd);
   
   echo "Bytes written: " . $bytes_written;
?> 

Output

When the above program is executed, it will produce the below output −

Bytes written: 25

Example 4

In the following example, we are using the dio_write() function for writing multiple strings to a file with the help of a loop.

   <?php
   // Open a file for writing
   $fd = dio_open('/PHP/PhpProjects/myfile.txt', O_WRONLY | O_CREAT);
   
   // Write data to the file in a loop
   for ($i = 1; $i <= 5; $i++) {
       $data = "Line $i\n";
       dio_write($fd, $data);
   }
   
   // Close the file
   dio_close($fd);
   
   echo "Data written to file.";
   ?> 

Output

This will create the below output −

Data written to file.
php_function_reference.htm
Advertisements