Anicky of Micropole Lyon Pole web
3/26/2016 - 5:32 PM

Récupère un fichier en "streaming"

Récupère un fichier en "streaming"

function utils_readfile_chunked($file, $filename = NULL, $retbytes = TRUE) {
    if (!$filename) {
        $filename = basename($file);
    }
    $mimetype = file_get_mimetype($file);
    header("Content-Type: " . $mimetype);
    header('Content-Disposition: attachment; filename=' . $filename);
    $buffer = "";
    $cnt = 0;
    $handle = fopen($file, "rb");
    if ($handle === false) {
        return false;
    }
    while (!feof($handle)) {
        $buffer = fread($handle, CHUNK_SIZE);
        echo $buffer;
        ob_flush();
        flush();
        if ($retbytes) {
            $cnt += strlen($buffer);
        }
    }
    $status = fclose($handle);
    if ($retbytes && $status) {
        return $cnt;
    }
    return $status;
}