PHP从数据库记录下载数据作为文件

时间:2015-05-02 06:09:23

标签: php

我从数据库表中获取数据。在这里,我想下载文件。文件下载怎么办? 如果我只下载一个记录文件,那么该文件只能下载。

这是我的代码:

<?php
    include_once 'config.php';
    $menu_name = 'report'; 
    $_SESSION['menu']= $menu_name; 

    $query="SELECT * FROM reports";
    $resultPhoto=$db->select($query);
    ?> 
    <div id="dyn2" class="shownpars">
        <a class="tOptions act" title="Options"><img src="images/icons/options.png" alt="" /></a>
        <table cellpadding="0" cellspacing="0" border="0" class="dTable">
            <thead>
                <tr>
                    <th>Sl. No.<span class="sorting" style="display: block;"></span></th>
                    <th>ClientName</th>
                    <th>Report</th>
                    <th>Status</th>
                    <th>DatePosted</th>
                    <th width="50">Action</th>
                </tr>
            </thead>
        <tbody>
        <?php
        $sl=0;
        if($resultPhoto && count($resultPhoto)>0)
        foreach($resultPhoto as $photo)
        {
            $sl+=1;  
             $req_date=date('d F Y', strtotime($photo['date_created']));                  
        ?>
            <tr class="gradeX">
                <td class="center"><?php echo $sl;?></td>
                <td class="center"><?php echo $photo['uid'];?></td>
                <td class="center"><?php echo $photo['report'];?></td>
                <td class="center">
                    <?php echo $photo['status'];?>
                </td>
                <td class="center"><?php echo $req_date;?></td>
                <td class="tableActs">
                    <a href="edit_photo.php?id=<?php echo $photo['report_id'];?>" class="tablectrl_small bDefault tipS" title="Edit"><span class="iconb" data-icon="&#xe1db;"></span></a>
                    <a onclick="return confirmDelete();" href="reports.php?id=<?php echo $photo['report_id'];?>&act=rm" class="tablectrl_small bDefault tipS" title="Remove"><span class="iconb" data-icon="&#xe136;"></span></a>
                </td>
            </tr>
            <?php }?>                
        </tbody>
        </table> 
    </div>
?>

1 个答案:

答案 0 :(得分:0)

您可以从数据库中选择文件属性和内容,然后打印/回显它。

<?php
$query="SELECT file_id, file_content, file_type, file_length, file_name FROM myFiles LIMIT 1";
$result=$db->select($query);

header('Content-Type: '. $result["file_type"]);
header('Content-Length: ' . $result["file_length"]);
header('Content-Disposition: filename='.$result["file_name"]);

flush();

print $result["file_content"];
flush();
exit;