PHP问题:filesize()返回0且文件中包含的数据很少? [英] PHP Problem : filesize() return 0 with file containing few data?

查看:46
本文介绍了PHP问题:filesize()返回0且文件中包含的数据很少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PHP调用Java命令,然后将其结果转发到一个名为result.txt的文件中.例如,该文件包含以下内容:结果是:5.0"但是函数filesize()返回0,而当我通过'ls -l'命令进行检查时,它也为0.因为当文件大小!= 0时,我决定将结果打印到屏幕上,所以什么也没打印.如何获得小数位数?或其他可用的解决方案?

I use PHP to call a Java command then forward its result into a file called result.txt. For ex, the file contains this: "Result is : 5.0" but the function filesize() returns 0 and when I check by 'ls -l' command it's also 0. Because I decide to print the result to the screen when file size != 0 so nothing is printed. How can I get the size in bit ? or another solution available?

推荐答案

在调用 filesize时,从 docs ,PHP会将结果缓存在统计信息缓存中.

From the docs, when you call filesize, PHP caches this result in the stat cache.

您是否尝试清除统计信息缓存?

Have you tried clearing the stat cache?

clearstatcache();

如果它不起作用,则可能的解决方法是打开文件,查找到其结尾,然后使用 ftell .

If it does not work, possible workaround is to open the file, seek to its end, and then use ftell.

$fp = fopen($filename, "rb");
fseek($fp, 0, SEEK_END);
$size = ftell($fp);
fclose($fp);

如果您实际上打算向用户显示输出,则可以先读取整个文件,然后再读取 strlen .

If you are actually planning to display the output to the user, you can instead read the entire file and then strlen.

$data = file_get_contents($filename);
$size = strlen($data);

这篇关于PHP问题:filesize()返回0且文件中包含的数据很少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆