52

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
} 
Scott B
  • 38,833
  • 65
  • 160
  • 266

5 Answers5

70

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.

Spliffster
  • 6,959
  • 2
  • 24
  • 19
26

Everybody be carefull when using filesize, cause it's results are cached for better performance. So if you need better precision, it is recommended to use something like:

<?
clearstatcache();
if(filesize($path_to_your_file)) {
    // your file is not empty
}

More info here

userlond
  • 3,632
  • 2
  • 36
  • 53
9

Using filesize() is clearly better. It uses stat() which doesn't have to open the file at all.

file_get_contents() reads the whole file.. imagine what happens if you have a 10GB file.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • 5
    Imagine how long your website would take to load with a 10GB CSS file :P – alex Jan 31 '11 at 23:13
  • 1
    I imagine, after further thought, there's probably less harm in just loading an empty css than going to the trouble of testing if its empty to avoid loading it. – Scott B Jan 31 '11 at 23:15
  • For small files (haven't read the CSS part) it doesn't really matter. filesize() is still faster and much better though - why would you want to actually read the file's contents from the harddisk if the inode (or even the filesystem's cache or PHP's internal stat-cache) contains all informations you need. – ThiefMaster Jan 31 '11 at 23:15
  • @ThieMaster The only reason I can think of is to determine if the CSS file actually contains information worth loading. If it is full of whitespace only, for example, there is not much point including it. I guess it is one of those speed/accuracy trade offs. +1 anyway for useful info. – alex Jan 31 '11 at 23:17
  • Simply do that when saving it: Strip comments and trim() it. If it's empty, don't save it at all. – ThiefMaster Jan 31 '11 at 23:19
  • @ThieMaster That would be ideal, but the OP says `I'm including a custom.css file in my template to allow site owners to add their own css rules` sounds like the file is edited directly. – alex Jan 31 '11 at 23:31
  • If the file is loaded from remote, both filesize and file_get_contents are bad even though filesize will probably just do a HTTP HEAD request.. but still nothing you'll want. – ThiefMaster Jan 31 '11 at 23:32
5

filesize() would be more efficient, however, it could be misleading. If someone were to just have comments in there or even just whitespace...it would make the filesize larger. IMO you should instead look for something specific in the file, like /* enabled=true */ on the first line and then use fopen/fread to just read the first line. If it's not there, don't load it.

Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87
CrayonViolent
  • 32,111
  • 5
  • 56
  • 79
  • nothing bad, however, in false result. this check is not that important – Your Common Sense Jan 31 '11 at 23:19
  • 1
    true...but neither is it a big deal to just load the file if it's empty. Just sayin'...if you're gonna nickel and dime something like loading an empty file, you may as well count this as "important" too – CrayonViolent Jan 31 '11 at 23:23
2

As mentioned in other answers, filesize is the way to go for local files. Many stream wrappers on the other hand, including HTTP, do not have stat() support, thus filesize will fail, whereas file_get_contents will work.

NikiC
  • 100,734
  • 37
  • 191
  • 225