php - When downloading file, its size is 0 bytes

844

When I try to download a file, it downloads but without content, i.e., its size is 0 bytes. I am using this code, what I am doing wrong?

$fullPath = "/home/yakar/www/files/gpx/".$file;

if (file_exists($fullPath)) {


    // setting headers
    header('Content-Description: File Transfer');
    header('Content-Type: '.$type);
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
} else {
    die('File does not exist');
}

People are also looking for solutions to the problem: php - How to pass multiple variables across multiple pages?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.