zoukankan      html  css  js  c++  java
  • PHP 文件下载流程

    前台HTML:

    添加download属性,不打开download.php页面

    <a style='color:blue' href='download.php' download='data/CrawlerID.txt' target=_blank>下载</a>

    download.php

    1:从数据库中导出内容

    2:fread输出到页面

    <?php
        //下载数据
        header('Content-Type:text/html;charset=utf-8');
        $crawlId=$_GET['id'];
        
        //导出sql语句:Select pid,price Into OutFile 'F:/QQPCmgr/Desktop/Data_OutFile.txt' fields terminated by ',' From `t_product`
        //导出文件命名格式Crawl_ID_txt;
        $filePath = "D:/WWW/amazon_crawl_system/data/";//此处给出你下载的文件在服务器的什么地方
        $fileName = "CrawlerID_".$crawlId.".txt";
        
        //删除文件
        if(file_exists($filePath.$fileName)){
            unlink($filePath.$fileName);
        }
        
        include_once('./mysql.php');
        $conndb=new ConnDB();
        $conndb->connect("127.0.0.1","root","root","amazon");
        
        //首先从t_crawl_url表中获取对应的id值,拼接sql语句
        $sql="select id from t_crawl_url where crawl_id=".$crawlId;
        $request=$conndb->queryarr($sql);
        $str='';
        if($request){
            foreach($request as $key => $values){
                $str.="crawl_url_id='".$values['id']."' or ";
            }
            $str=substr($str,0,strlen($str)-4);
            //echo $str;
        }else{
            echo "error";
            exit;
        }
        
        $sql="select pid,price Into OutFile '".$filePath.$fileName."' fields terminated by ',' lines terminated by '
    ' From `t_product` where ".$str;
        
        $request=$conndb->query($sql);
        if($request){
            /*echo "<script> window.location.href='download.php?id=".$crawlId."&file=".$saveFile."';</script>";*/
        }else{
            echo "<script> alert('下载失败');</script>";
            exit;
        }
        
        //此处给出你下载的文件名
        $file = fopen($filePath.$fileName, "r"); //打开文件
        //输入文件标签
        header("Content-type:application/octet-stream ");
        header("Accept-Ranges:bytes ");
        header("Accept-Length:   " . filesize($filePath.$fileName));
        header("Content-Disposition:   attachment;   filename= ".$fileName);
        
        //输出文件内容
        echo fread($file, filesize($filePath . $fileName));
        fclose($file);
        
    
    ?>
  • 相关阅读:
    BZOJ4896 THUSC2016补退选(trie)
    BZOJ4892 Tjoi2017dna(后缀数组)
    BZOJ4890 Tjoi2017城市
    BZOJ4888 Tjoi2017异或和(树状数组)
    BZOJ4887 Tjoi2017可乐(动态规划+矩阵快速幂)
    BZOJ4883 棋盘上的守卫(环套树+最小生成树)
    BZOJ4881 线段游戏(二分图+树状数组/动态规划+线段树)
    BZOJ4878 挑战NP-Hard(dfs树)
    BZOJ5466 NOIP2018保卫王国(倍增+树形dp)
    BZOJ4873 Shoi2017寿司餐厅(最小割)
  • 原文地址:https://www.cnblogs.com/zeze/p/5896635.html
Copyright © 2011-2022 走看看