user3155224
user3155224

Reputation: 13

Download file php in the server

in the localhost i get everything perfect but when i upload it to the server i get this error

Warning: Cannot modify header information - headers already sent by( hier is my code

    <?php

function download($file){
    $dir = './download/';
    $path = $dir.$file;
    if(!file_exists($path)){
        die('Error');
    }else{
        header('Content-Description : File Transfer');
        header('Content-Disposition : attachment; filename='.basename($path));
        header('Content-Type: application/octet-stream');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        ob_start();
        flush();
        readfile($path);
        exit;
    }
}
if (isset($_GET['download'])) {
    if (!empty($_GET['download'])) {
        $file = $_GET['download'];
        download($file);
    }
}
 ?>

<a class="download-template" href="example.php?download=Modern.rar">Download</a>

Upvotes: 0

Views: 1237

Answers (3)

    Use the following code for download any type of file extensions(including .php,.html):
    <?php 
    $filename = $test_data['test_name'];
    $contenttype = "application/octet-stream";
    header("Content-Type: " . $contenttype);
    header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\";");
    readfile(ADMIN_ROOT.'modules/tests/test_pdfs/'.$filename);
    exit();
    ?>

Or you can check in this link working example:
http://websamplenow.com/29/file_download

Upvotes: 0

Zarkoeleto
Zarkoeleto

Reputation: 133

Change the file encoding to "without BOM" (e.g. using notepad++) or remove the BOM before

Upvotes: 0

Jake N
Jake N

Reputation: 10583

Remove the whitespace before the <?php

Like so

<?php

function download($file){
    $dir = './download/';
    $path = $dir.$file;
    if(!file_exists($path)){
        die('Error');
    }else{
        header('Content-Description : File Transfer');
        header('Content-Disposition : attachment; filename='.basename($path));
        header('Content-Type: application/octet-stream');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        ob_start();
        flush();
        readfile($path);
        exit;
    }
}
if (isset($_GET['download'])) {
    if (!empty($_GET['download'])) {
        $file = $_GET['download'];
        download($file);
    }
}
 ?>

<a class="download-template" href="example.php?download=Modern.rar">Download</a>

The error is telling you that you're outputting content before it should do.

If you output content then the page headers have already been sent, so your call to header() will fail because the headers have already gone. And headers are always sent first.

By removing the whitespace there is no content to send, so the headers are not sent, so the call to header will then work and not error.

Upvotes: 3

Related Questions