douhao2153 2011-04-01 01:04
浏览 47
已采纳

将mysql单元格转换为文本文件,允许用户下载,然后删除文本文件

how can i make a function in php that, when a link was clicked it would download a certain cell in a mySQL table and allow the user to download it as a .txt file? When the download is complete, the file is deleted.

  • 写回答

3条回答 默认 最新

  • dqq9695 2011-04-01 01:23
    关注

    I don't think you'd need to create/delete any files...

    Say you have a link on your page:

    <a href="download.php?id=whatever">Download this as a txt file</a>
    

    I'm not sure how you're identifying the cells that the user wants to download, but I'll trust that you're doing it securely and efficiently.

    Somehow, you have the table name, the row, and the column of the MySQL cell. So you just do a simple query...

    $result = mysqli_query('select the cell', $your_db);
    $result = mysqli_fetch_assoc($result);
    $result = $result['cell_name'];
    

    This gives you the value of that cell as a simple PHP variable. Now all you need to do is output it to the user:

    header('content-type: text/plain');
    echo $result;
    

    That should print the value of the cell to the screen, as simple text. The user could then save the page. :D

    Edit - Force Download

    I haven't tested this, but the basic idea would be to add some more headers:

    header('content-type: text/plain');
    header('Content-Disposition: attachment; filename=whatever.txt');
    header('Pragma: no-cache');
    echo $result;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?