SO friends... i had a fight with this code.. please help :(

<? ob_start(); ?>
<?php

// Make sure an ID was passed
if(isset($_GET['id'])) {
// Get the ID
    $id = intval($_GET['id']);

    // Make sure the ID is in fact a valid ID
    if($id <= 0) {
        die('The ID is invalid!');
    }
    else {
        // Connect to the database
        $dbLink = new mysqli('xxxxx', 'xxxx', 'xxxxx', 'xxxxx');
        if(mysqli_connect_errno()) {
            die("MySQL connection failed: ". mysqli_connect_error());
        }

        // Fetch the file information
        $query = "
            SELECT `mime`, `name`, `size`, `data`
            FROM `file`
            WHERE `id` = {$id}";
        $result = $dbLink->query($query);

        if($result) {
            // Make sure the result is valid
            if($result->num_rows == 1) {
            // Get the row
                $row = mysqli_fetch_assoc($result);

                // Print headers

                header("Location: ". $row['mime']);
                header("Location: ". $row['size']);
                header("Location: attachment; filename= ". $row['name']);


                // Print data
                echo $row['data'];
            }
            else {
                echo 'Error! No image exists with that ID.';
            }

            // Free the mysqli resources
            @mysqli_free_result($result);
        }
        else {
            echo "Error! Query failed: <pre>{$dbLink->error}</pre>";
        }
        @mysqli_close($dbLink);
    }
}
else {
    echo 'Error! No ID was passed.';
}

?>
<? ob_flush(); ?>



And the error..

Warning: Cannot modify header information - headers already sent by (output started at /home/topname/public_html/d/get_file.php:3) in /home/topname/public_html/d/get_file.php on line 34

Warning: Cannot modify header information - headers already sent by (output started at /home/topname/public_html/d/get_file.php:3) in /home/topname/public_html/d/get_file.php on line 35

Warning: Cannot modify header information - headers already sent by (output started at /home/topname/public_html/d/get_file.php:3) in /home/topname/public_html/d/get_file.php on line 36

What can i do?

Recommended Answers

All 8 Replies

Simplest: remove ob_start and ob_flush. The first one is preventing you from setting the headers.

You seem indeed to be using the output buffer while there is nothing you are buffering for output :). What an output buffer does is, shortly explained, this:

Instead of writing your output to the screen, it buffers it. It allows you to set headers and do other stuff in the meanwhile, and when everything you needed to do before outputting stuff to your the has been done, you flush the output buffer and output what's inside it to the screen. In your scenario there is nothing to be buffered before your headers are set, so you probably don't need those output buffers and can remove them if you want.

Removed the ob_start and ob_flush ... still same error

Member Avatar for diafol

Do you have a blank line (on line 3) before your php tags? Or maybe html?

Any screen output (whitespace, html or php-derived output, eg. error message, echo, print etc) will cause header() redirect to fail.

Anyway:

header("Location: ". $row['mime']);
header("Location: ". $row['size']);
header("Location: attachment; filename= ". $row['name']);

A 'location' in the header will cause redirect, so nothing below that line will run. So I don't understand what you hope to achieve with the other two headers.

Look tried this... no black line on line 3

                header("Content-Type: ". $row['mime']);
                header("Content-Length: ". $row['size']);
                header("Content-Disposition: attachment; filename=". $row['name']);


                What is wrong here.. could be buffer overflow?

Re: finaly solve it :D it was an echo who caused the crash :D

Member Avatar for diafol

Yep, that'll do it. Solved?

Solved thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.