ftruncate
Description
The ftruncate of Filesystem for PHP truncates a file to a given length.
Syntax
ftruncate( resource $stream, int $size ): bool
Parameters
stream
The file pointer.
NOTE: The stream must be open for writing.
size
The size to truncate to.
NOTE: If size is larger than the file then the file is extended with null bytes.
NOTE: If size is smaller than the file then the file is truncated to that size.
Return
Returns true on success or false on failure.
Examples
1 · stream size
<? $filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/txt/1.txt'; $mode = 'r+'; $stream = fopen($filename, $mode); $size = 2; $return = ftruncate($stream, $size); var_export($return); fclose($stream);
true
2
<? $filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/txt/1.txt'; $mode = 'r+'; $stream = fopen($filename, $mode); echo fread($stream, filesize($filename)) . PHP_EOL; $size = 2; ftruncate($stream, $size); rewind($stream); echo fread($stream, filesize($filename)); fclose($stream);
hello he
Links
Filesystem
- basename
- chgrp
- chmod
- chown
- clearstatcache
- copy
- dirname
- disk_free_space
- disk_total_space
- diskfreespace
- fclose
- feof
- fflush
- fgetc
- fgetcsv
- fgets
- file
- file_exists
- file_get_contents
- file_put_contents
- fileatime
- filectime
- filegroup
- fileinode
- filemtime
- fileowner
- fileperms
- filesize
- filetype
- flock
- fnmatch
- fopen
- fpassthru
- fputcsv
- fputs
- fread
- fscanf
- fseek
- fstat
- ftell
- fwrite
- glob
- is_dir
- is_executable
- is_file
- is_link
- is_readable
- is_uploaded_file
- is_writable
- is_writeable
- lchgrp
- lchown
- link
- linkinfo
- lstat
- mkdir
- move_uploaded_file
- pathinfo
- pclose
- popen
- readfile
- readlink
- realpath
- realpath_cache_get
- realpath_cache_size
- rename
- rewind
- rmdir
- set_file_buffer
- stat
- symlink
- tempnam
- tmpfile
- touch
- umask
- unlink