确定文件是否为空的最佳方法(php)? [英] Best way to determine if a file is empty (php)?
本文介绍了确定文件是否为空的最佳方法(php)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在模板中包含一个custom.css文件,以允许网站所有者添加自己的CSS规则.但是,当我发送文件时,该文件为空,如果他们没有向其中添加任何规则,则无法加载该文件.
I'm including a custom.css file in my template to allow site owners to add their own css rules. However, when I ship the file, its empty and there's no sense loading it if they've not added any rules to it.
确定其是否为空的最佳方法是什么?
What's the best way to determine if its empty?
if ( 0 == filesize( $file_path ) )
{
// file is empty
}
// OR:
if ( '' == file_get_contents( $file_path ) )
{
// file is empty
}
推荐答案
file_get_contents()
将读取整个文件,而filesize()
使用stat()
确定文件大小.使用filesize()
,它将消耗更少的磁盘I/O和更少的内存.
file_get_contents()
will read the whole file while filesize()
uses stat()
to determine the file size. Use filesize()
, it should consume less disk I/O and much less memory.
这篇关于确定文件是否为空的最佳方法(php)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文