Scott B Scott B - 26 days ago 9
PHP Question

Best way to determine if a file is empty (php)?

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
}

Answer

file_get_contents() will read the whole file while filesize() uses stat() to detirmine the file size. Use filesize(), it should consume less disk I/O.

Comments