Open In App

PHP | ftruncate( ) Function

Last Updated : 19 Jun, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The ftruncate() function in PHP is an inbuilt function which is used to truncate(shorten) an open file to the specified length. The file and the new size of the file are sent as parameters to the ftruncate() function and it returns True on success and False on Failure. If size specified in the parameter is larger than the file then the file is extended with null bytes and if size specified is smaller than the file then the file is truncated to that size.

Syntax :

ftruncate(file, size)

Parameters: The ftruncate() function in PHP accepts two parameters.

  1. file: It is a mandatory parameter which specifies the file.
  2. size : It is a mandatory parameter which specifies the new size of the file.

Return Value: It returns True on success and False on Failure.

Exceptions:

  1. rewind() function must be used after ftruncate() function to replace file content.
  2. The file pointer is not changed by the ftruncate() function.
  3. If size specified in the parameter is larger than the file then the file is extended with null bytes and if size specified is smaller than the file then the file is truncated to that size

Below programs illustrate the ftruncate() function:

Program 1:




<?php
// checking filesize before truncating
echo filesize("gfg.txt");
  
// Opening the file
$myfile = fopen("gfg.txt", "a+");
  
// truncating the file
ftruncate($myfile, 10);
  
// closing the file
fclose($file);
  
// Clearing cache and checking filesize again
clearstatcache();
echo filesize("gfg.txt");
  
// closing the file
fclose($myfile);
?>


Output:

500
10

Program 2:




<?php
$myfile = 'gfg.txt';
  
// opening file in read mode
$myhandle = fopen($myfile, 'r+');
  
// truncating the file
ftruncate($myhandle, rand(1, filesize($myfile)));
  
// using reiwnd() to replace file content
rewind($myhandle);
echo fread($myhandle, filesize($myfile));
  
// closing the file
fclose($handle);
  
?>


Output:

10

Reference : http://php.net/manual/en/function.ftruncate.php



Similar Reads

PHP | SplFileObject ftruncate() Function
The SplFileObject::ftruncate() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used to truncates the file size in bytes. Syntax: bool SplFileObject::ftruncate( $length ) Parameters: This function accept single parameter $length which specified the length of truncate of the file. Return values: This function returns Tru
1 min read
Node.js fs.ftruncate() Method
The fs.ftruncate() method is used to change the size of the file i.e. either increase or decrease the file size. It changes the length of the file at the path by len bytes. If len is shorter than the file’s current length, the file is truncated to that length. If it is greater than the file length, it is padded by appending null bytes (x00) until l
2 min read
How to get the function name inside a function in PHP ?
To get the function name inside the PHP function we need to use Magic constants(__FUNCTION__). Magic constants: Magic constants are the predefined constants in PHP which is used on the basis of their use. These constants are starts and end with a double underscore (__). These constants are created by various extensions. Syntax: $string = __FUNCTION
1 min read
How the User-Defined Function differs from Built-in Function in PHP ?
In PHP, functions are blocks of reusable code that perform specific tasks. They enhance code readability, modularity, and maintainability by encapsulating logic into named units. PHP functions can be categorized into two main types: user-defined functions and built-in functions. User-Defined FunctionsUser-defined functions are functions created by
2 min read
PHP 5 vs PHP 7
PHP is a server side scripting language designed for web development by Rasmus Lerdorf in 1994. Since its launch in 1994 PHP has become an industry standard supporting almost 80% of the websites ( 79.8% to be precise) with its closest competitor being ASP.Net at 19.8% and others like Ruby, Java trailing far behind. The PHP development team released
4 min read
PHP | Get PHP configuration information using phpinfo()
PHP provides us with a built-in function phpinfo() which gives us the details about the PHP version and PHP configuration of PHP installed in our system. To know about the Configurations and PHP version which is installed in your computer, a simple PHP script can be used. The script consists of a PHP function called "phpinfo()" which outputs inform
2 min read
PHP | php.ini File Configuration
At the time of PHP installation, php.ini is a special file provided as a default configuration file. It's very essential configuration file which controls, what a user can or cannot do with the website. Each time PHP is initialized, the php.ini file is read by the system. Sometimes you need to change the behavior of PHP at runtime, then this config
5 min read
How to import config.php file in a PHP script ?
The include statement in PHP copies the code of text from the file mentioned, into the file that uses the include statement. It directs the preprocessor to insert the content specified into the following program. The name of the file to be included is written in double-quotes. It is a good practice to write the basic database details and user detai
2 min read
How to include content of a PHP file into another PHP file ?
Including the content of a PHP file into another file reduces the complexity of code by reducing the code and improving its modularity by splitting the code into different files so that it is easy to understand and manage. There are two ways to do it by using the following PHP functions. PHP include() Function: It will show the warning if the file
2 min read
PHP | imagecreatetruecolor() Function
The imagecreatetruecolor() function is an inbuilt function in PHP which is used to create a new true-color image. This function returns a blank image of the given size. Syntax: resource imagecreatetruecolor( $width, $height ) Parameters: This function accepts two parameters as mentioned above and described below: $width: This parameter is used to s
2 min read