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

    方式一

    <?php
    $file = 'monkey.gif';
    
    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="'.basename($file).'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        readfile($file);
        exit;
    }
    ?>

    方式二

    <?php
    $local_file = 'file.zip';
    
    // set the download rate limit (=> 20,5 kb/s)
    $download_rate = 20.5;
    if(file_exists($local_file) && is_file($local_file))
    {
        header('Cache-control: private');
        header('Content-Type: application/octet-stream');
        header('Content-Length: '.filesize($local_file));
        header('Content-Disposition: filename='.$local_file);
    
        flush();
        $file = fopen($local_file, "r");
        while(!feof($file))
        {
            // send the current file part to the browser
            print fread($file, round($download_rate * 1024));
            // flush the content to the browser
            flush();
            // sleep one second
            sleep(1);
        }
        fclose($file);
    }
    else {
        die('Error: The file '.$local_file.' does not exist!');
    }
    ?>
  • 相关阅读:
    js学习之函数
    面试题
    渐进增强(progressive enhancement)、优雅降级(graceful degradation)
    倒计时
    css 平行四边形
    网址URL分解
    图片延时加载
    获取元素的宽高,左边距上边距
    电商平台放大镜效果
    js笔记
  • 原文地址:https://www.cnblogs.com/zhangxuechao/p/11709361.html
Copyright © 2011-2022 走看看