ftruncate()

创建于 2024-12-03 / 25
字体: [默认] [大] [更大]
❮ PHP 文件系统参考手册

实例

将打开的文件截断为指定长度(100 字节):

<?php
// Check filesize
echo filesize("test.txt");
echo "<br>";

$file = fopen("test.txt", "a+");
ftruncate($file,100);
fclose($file);

// Clear cache and check filesize again
clearstatcache();
echo filesize("test.txt");
?>

上述代码的输出为:

792
100

定义和用法

ftruncate() 函数把文件截断到指定的长度。

语法

ftruncate(file, size)

参数值

参数 描述
file 必需。规定要截断的打开文件。
size 必需。规定新的文件大小。

说明

接受文件指针 file 作为参数,并将文件大小截取为 size。如果成功则返回 TRUE,否则返回 FALSE。


提示和注释

注释:文件只会在 append 模式下改变。在 write 模式下,必须加上 fseek() 操作。

注释:在 PHP 4.3.3 之前,ftruncate() 在成功时返回一个整数值 1,而不是布尔值的 TRUE。


技术细节

返回值: 成功时为 TRUE,失败时为 FALSE。
PHP 版本: 4.0+

❮ PHP 文件系统参考手册
0 人点赞过