Securing/Escaping Output of file content - reading via fread() in PHP

I am working on securing the content read from a file via the fread() function.

private function readfile_chunked($file) {
    $chunksize = 1024 * 1024;

    // Open Resume
    $handle = @fopen($file, 'r');

    if (false === $handle) {
        return FALSE;
    }

    while (!@feof($handle)) {
        $content  = @fread($handle, $chunksize);
        echo wp_kses_post( $content);

        if (ob_get_length()) {
            ob_flush();
            flush();
        }
    }

    return @fclose($handle);
}

The aforementioned wp_kses_post($content) is suggested by the WP plugin review team to secure the file content as well. But this solution is not working for me. It is downloading the file in an infinite loop. Any help will be appreciated on How we can escape output of fread() in WordPress?. Or any alternative function so we can skip the echo function.

Topic escaping plugin-development security Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.