Download files from MYSQL database

后端 未结 2 1332
-上瘾入骨i
-上瘾入骨i 2021-01-28 01:36

ok i got files uploading to my database alright. but having problems downloading files from the database, it shows as a link and when i click on the link nothing happens.

<
相关标签:
2条回答
  • In your download.php you make the links and exit. This means you will not go any further than your exit call.

    <?php
       }
       }
       exit;
       mysql_close()
     ?>
    

    You need to rethink your logic in the download file. You also call

    header("Content-length: $size");
    exit;
    print $content;
    
    0 讨论(0)
  • 2021-01-28 02:14

    I scan thoroughly your code and here is the final answer.

    <?php
        require 'connect.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)){
                echo "<a href=\"download.php?id=\$id\">$name</a><br>";
            }
        }
    
        if(isset($_GET['id'])){
            $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_row($result);
            header("Content-Disposition: attachment; filename=\"$name\"");
            header("Content-type: $type");
            header("Content-length: $size");
            print $content;
        }
    ?>
    
    0 讨论(0)
提交回复
热议问题