0

I want to create a upload and download function in my website. The upload script is working but the download script is not working. I am not getting any error but once the files are retrieve from the database i couldn't download it. If i click on the file the file content open in the same page.

        <?php
        $query = "SELECT id, name FROM upload";
        $result = mysql_query($query) or die('Error, query failed');
        if(mysql_num_rows($result) == 0) {
            echo "Database is empty <br>";
        } else {
            while(list($id, $name) = mysql_fetch_array($result)) {
                ?>
                <a href="download.php?id=<?php echo urlencode($id);?>"><?php echo urlencode($name);?></a> <br>
                <?php
            }
        }
        mysql_close();
        ?>
    </body>
</html>
<?php
if(isset($_GET['id'])) {
    // if id is set then get the file with the id from database
    $id    = $_GET['id'];
    $query = "SELECT name, type, size, content " .
    "FROM upload WHERE id = '$id'";
    $result = mysql_query($query) or die('Error, query failed');
    list($name, $type, $size, $content) = mysql_fetch_array($result);
    header("Content-length: $size");
    header("Content-type: $type");
    header("Content-Disposition: attachment; filename=$name");
    ob_clean();
    flush();
    echo $content;
    mysql_close();
    exit;
}
?>

This is the url to the download site: download

Cœur
  • 37,241
  • 25
  • 195
  • 267
raaj5671
  • 105
  • 4
  • 12
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](http://j.mp/XqV7Lp). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – esqew Jul 17 '14 at 04:11

1 Answers1

1

You can't set any headers after output has been sent to the browser. This has clearly been done by the time your last lot of code is executed, because you've got HTML code above it.

It should work by putting your code at the top of your script, instead of the bottom.

Error wise, it sounds like you aren't getting any errors. You should turn on error reporting, at the very least on your local/development environment:

error_reporting(E_ALL);

This would likely tell you the cause of your issue straight away and help you diagnose it quickly.

Community
  • 1
  • 1
scrowler
  • 24,273
  • 9
  • 60
  • 92
  • its still not showing any errors. its still showing up the content – raaj5671 Jul 17 '14 at 04:19
  • Do some debugging, put little echo statements throughout your script and track its execution progress until it stops. – scrowler Jul 17 '14 at 04:23
  • i did some debugging but still its the same, there is no error. Did you try to click on download link which i post in the question? – raaj5671 Jul 17 '14 at 04:36
  • Actually i got the script from this site http://www.icraftzone.com/2012/06/file-upload-and-download-script-using.html – raaj5671 Jul 17 '14 at 04:42
  • Try getting rid of the output buffering calls: `ob_clean()` and `flush()` - not necessary – scrowler Jul 17 '14 at 05:07