+3 votes
in Programming Languages by (56.7k 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

+1 vote
by (349k points)
selected by
 
Best answer

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");
}

?>


...