PHP - Direct I/O dio_truncate() Function



The PHP Direct I/O dio_truncate() function is used to truncate (shorten) a file to a given length. It means that the file can get smaller if the content is removed after a set period of time.

Syntax

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

bool dio_truncate (resource $fd, int $length)

Parameters

Below are the parameters of the dio_truncate() function −

  • $fd − It is a resource that is typically obtained using dio_open().

  • $length − It is the length to which the file should be truncated. This is an integer.

Return Value

The dio_truncate() function returns TRUE on success and FALSE on failure.

PHP Version

First introduced in core PHP 4.3.0, the dio_truncate() function continues to function easily in PHP 5.1.0.

Example 1

First we will show you the basic example of the PHP Direct I/O dio_truncate() function to open a file and truncate it to 10 bytes.


Output

The above code will result something like this −

The file is truncated successfully!

Example 2

In the below PHP code we will try to use the dio_truncate() function and we truncate a file to zero bytes by emptying it.

 

Output

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

Truncated a File to Zero Bytes

Example 3

Now the below code to handle errors when truncating a file using the dio_truncate() function, and prints the error message.

 

Output

This will create the below output −

Error truncating file
php_function_reference.htm
Advertisements