ftruncate

(PHP 4, PHP 5, PHP 7, PHP 8)

ftruncate将文件截断到指定的长度

说明

ftruncate(resource $stream, int $size): bool

接受文件指针 stream 作为参数,并将文件大小截取为 size

参数

stream

文件指针。

注意:

stream 必须打开写入。

size

截断的大小。

注意:

If size is larger than the file then the file is extended with null bytes.

If size is smaller than the file then the file is truncated to that size.

返回值

成功时返回 true, 或者在失败时返回 false

范例

示例 #1 文件截取示例

<?php
$filename
= 'lorem_ipsum.txt';

$handle = fopen($filename, 'r+');
ftruncate($handle, rand(1, filesize($filename)));
rewind($handle);
echo
fread($handle, filesize($filename));
fclose($handle);
?>

注释

注意:

文件指针不会改变。

参见

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top