PHP | SplFileObject ftruncate() Function
Last Updated :
20 Dec, 2018
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 True on success or False on failure.
Below Programs illustrate the SplFileObject ftruncate() function in PHP:
Program 1:
<?php
$gfg = new SplFileObject( "gfg.txt" , "w+" );
$gfg ->fwrite( "GeeksforGeeks" );
$gfg ->ftruncate(8);
$gfg -> rewind ();
echo $gfg -> fgets ();
?>
|
Output:
Geeksfor
Program 2:
<?php
$GFG = array (
"dummy.txt" ,
"gfg.txt" ,
"frame.txt"
);
foreach ( $GFG as & $arr ) {
$file = new SplFileObject( $arr );
$file ->ftruncate(8);
$file -> rewind ();
echo $file -> fgets ();
}
?>
|
Output:
Geeksfor
Contribu
Article
Reference: http://php.net/manual/en/splfileobject.ftruncate.php
Share your thoughts in the comments
Please Login to comment...