为什么SplFileInfo返回错误的大小

时间:2018-03-19 15:37:15

标签: php file io

读取文件大小后,您可以更改文件,返回的大小始终相同。

为什么?如何让PHP返回实际数据?

我的PHP版本(今天在Windows上)

$ php -v
PHP 7.2.0 (cli) (built: Nov 28 2017 23:48:32) ( NTS MSVC15 (Visual C++ 2017) x64 )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2017 Zend Technologies

测试代码

<?php
$fn = 'myfile.txt';
file_put_contents($fn, "abcd\n");           // I have some file somewhere ...
$fi = new \SplFileInfo($fn);
echo 'Size is ', $fi->getSize(), PHP_EOL;   // Ok, size is 5
file_put_contents($fn, "abcd\nefgh\n");     // Putsize=10 content
echo 'Size is ', $fi->getSize(), PHP_EOL;   // Still shows 5
unset($fi);                                 // Free info object
$fi = new \SplFileInfo($fn);                // And start over,
echo 'Size is ', $fi->getSize(), PHP_EOL;   // But still 5
$fw = $fi->openFile('w');                   // Write with objects because
$fw->fwrite("abcd\nefgh\nijkl\n");          // maybe they share fileinfo data
echo 'Size is ', $fw->getSize(), PHP_EOL;   // but still has file = 5
echo 'filesize() ', filesize($fn), PHP_EOL; // Alternative function

输出

Size is 5
Size is 5
Size is 5
Size is 5
filesize() 5

这对你有意义,但对我来说肯定不是。我正在思考PHP缓存的数据,因此我尝试使用unset()触发析构函数,但它没有效果。

那么,如何告诉PHP给我我想要的东西?

谢谢

0 个答案:

没有答案