首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PHP从数据库记录以文件形式下载数据

PHP从数据库记录以文件形式下载数据
EN

Stack Overflow用户
提问于 2015-05-02 06:09:22
回答 1查看 125关注 0票数 1

我从数据库表中获取数据。这里我想下载文件。如何做文件下载?如果我只下载一个记录文件,该文件只应该下载。

这是我的代码:

代码语言:javascript
运行
复制
<?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>
?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-04 13:46:09

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

代码语言:javascript
运行
复制
<?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;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29999082

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档