+2 votes
in Programming Languages by (6.4k points)
I want to delete all image files from a folder if the file size is less than 100KB. How can I check the file size in PHP?

1 Answer

0 votes
by (26.1k points)

You can use PHP function filesize() to get the size of the file in bytes. This function requires a filename as an argument and returns file size in bytes on success, FALSE on failure.

Here is an example to check the size and delete a file using PHP.

<?php

$filename = "phpinfo.php";
if (filesize($filename) < 100000){
    if (unlink($filename)){
        print("File was deleted successfully\n");
    }else{
        print("Something went wrong\n");
    }
}else{
    print("File is bigger than 100k\n");
}

?>

...