Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 }  
like image 581
Scott B Avatar asked Jan 31 '11 23:01

Scott B


People also ask

How do you check if the file is empty in PHP?

PHP empty() Function The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true.

How do you check if a line is empty in PHP?

Answer: Use the PHP empty() function You can use the PHP empty() function to find out whether a variable is empty or not. A variable is considered empty if it does not exist or if its value equals FALSE .

Is null or empty PHP?

is_null() The empty() function returns true if the value of a variable evaluates to false . This could mean the empty string, NULL , the integer 0 , or an array with no elements. On the other hand, is_null() will return true only if the variable has the value NULL .

How do you empty a file in PHP?

To delete a file in PHP, use the unlink function. Let's go through an example to see how it works. The first argument of the unlink function is a filename which you want to delete. The unlink function returns either TRUE or FALSE , depending on whether the delete operation was successful.


1 Answers

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.

like image 134
Spliffster Avatar answered Oct 01 '22 21:10

Spliffster