zoukankan      html  css  js  c++  java
  • exforce_download() 对CI中force_download()增强后的函数(不依赖CI)

    /**
    * Enhanced force_download, can accept file in server
    * @author Wilson Zeng
    * @param $filename | The name that will use as the download file's name by default
    * @param $data | Can be: [1]. a file real path; [2]. an string that contain the file's content
    * @param $is_file | To specify the type of the $data
    * @return Byte Stream
    */
    function exforce_download($filename = '', $data = '', $is_file = FALSE)
    {
    if ($filename == '' OR $data == '')
    {
    return FALSE;
    }

    if($is_file && !is_file($data)){
    return FALSE;
    }

    // Try to determine if the filename includes a file extension.
    // We need it in order to set the MIME type

    if (FALSE === strpos($filename, '.'))
    {
    return FALSE;
    }

    // Grab the file extension
    $x = explode('.', $filename);
    $extension = end($x);

    // Load the mime types
    /*zcs=Reduce Dependence
    if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes'.EXT))
    {
    include(APPPATH.'config/'.ENVIRONMENT.'/mimes'.EXT);
    }
    elseif (is_file(APPPATH.'config/mimes'.EXT))
    {
    include(APPPATH.'config/mimes'.EXT);
    }
    */
    $mimes = array(); //zcs=[NOTE] Here we just assign an empty array to reduce the dependence of the CI framework

    // Set a default mime if we can't find it

    if ( ! isset($mimes[$extension]))
    {
    $mime = 'application/octet-stream';
    }
    else
    {
    $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
    }

    $size = 0;
    if($is_file){
    $size = filesize($data);
    }else{
    $size = strlen($data);
    }

    // Generate the server headers
    if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE") !== FALSE)
    {
    header('Content-Type: "'.$mime.'"');
    header('Content-Disposition: attachment; filename="'.$filename.'"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header("Content-Transfer-Encoding: binary");
    header('Pragma: public');
    header("Content-Length: ".$size);
    }
    else
    {
    header('Content-Type: "'.$mime.'"');
    header('Content-Disposition: attachment; filename="'.$filename.'"');
    header("Content-Transfer-Encoding: binary");
    header('Expires: 0');
    header('Pragma: no-cache');
    header("Content-Length: ".$size);
    }

    if($is_file){
    $h_file = fopen($data, 'r');
    while(!feof($h_file)){
    echo fread($h_file, 1048576);//1M
    }
    fclose($h_file);
    }else{
    exit($data);
    }
    }
  • 相关阅读:
    音频播放器
    SQL Server找不到配置管理器怎么办
    SQL——游标循环的写法
    SQL——多条相似内容只取一条
    SQL——delete left join
    SQL——查询包含某字段的所有表
    SQL——获取数据库表结构
    SQL Server数据库改名
    SQL——left join的结果行数可能大于左表
    SQL——用临时表代替过多的变量声明赋值
  • 原文地址:https://www.cnblogs.com/ppoo24/p/2324537.html
Copyright © 2011-2022 走看看