Change language

PHP Ftruncate () Function

Syntax:
ftruncate (file, size) 
Parameters:The ftruncate() function in PHP takes two parameters .
  • file : This is a required parameter that specifies the file.
  • size : This is a required parameter that specifies new file size.
  • Returned value:Returns True on success and False on failure.Exceptions :
  • The rewind() function should be used after ftruncate() to replace the contents of a file.
  • The file pointer is not changed by ftruncate().
  • If the size specified in the parameter is larger than the file, then the file is expanded with zero bytes, and if the specified size is smaller than the file, then the file is truncated to this size
  • The programs below illustrate the ftruncate() function: Program 1 :
    // checking file size before truncating echo filesize ( "gfg.txt" );  
    // Open file $myfile = fopen ( "gfg.txt" , "a +" );  
    // truncate file ftruncate ( $myfile , 10);  
    // close the file fclose ( $file );  
    // Clear cache and check file size again
    clearstatcache(); echo filesize ( "gfg.txt" );  
    // close the file fclose ( $myfile ); ?>
    Output:
    500 10 
    Program 2 : $myfile = ’gfg.txt’ ;  
    // open file in read mode $myhandle = fopen ( $myfile , ’ r + ’ );  
    // truncate file ftruncate ( $myhandle , rand (1, filesize ( $myfile )));  
    // using reiwnd() to replace file content rewind ( $myhandle ); echo fread ( $myhandle , ( $myfile ));  
    // close the file fclose ( $handle );  
    ?>
    Output:
    10 
    Link: http://php.net /manual/en/function.ftruncate.php

    Shop

    Learn programming in R: courses

    $

    Best Python online courses for 2022

    $

    Best laptop for Fortnite

    $

    Best laptop for Excel

    $

    Best laptop for Solidworks

    $

    Best laptop for Roblox

    $

    Best computer for crypto mining

    $

    Best laptop for Sims 4

    $

    Latest questions

    NUMPYNUMPY

    psycopg2: insert multiple rows with one query

    12 answers

    NUMPYNUMPY

    How to convert Nonetype to int or string?

    12 answers

    NUMPYNUMPY

    How to specify multiple return types using type-hints

    12 answers

    NUMPYNUMPY

    Javascript Error: IPython is not defined in JupyterLab

    12 answers


    Wiki

    Python OpenCV | cv2.putText () method

    numpy.arctan2 () in Python

    Python | os.path.realpath () method

    Python OpenCV | cv2.circle () method

    Python OpenCV cv2.cvtColor () method

    Python - Move item to the end of the list

    time.perf_counter () function in Python

    Check if one list is a subset of another in Python

    Python os.path.join () method