php文件下载函数

/**
 * 利于url传输的base64编码函数 
 * @param type $str 字符串
 * @param type $encryption_str  加密字符串
 * @return type 
 */
function self_base64_encode($str, $str_enc) {
    $str = strval($str);
    $str_enc = strval($str_enc);
    if (empty($str) && $str !== '0') {
        return false;
    }
    $str = base64_encode($str . $str_enc);
    if ($str || $str === '0') {
        return strtr($str, array('+' => '-', '/' => '_', '=' => ''));
    }
    return false;
}

/**
 * 利于url传输的base64解码码函数
 * @param type $str 
 * @param type $encryption_str  加密字符串
 * @return type 
 */
function self_base64_decode($str64, $str_enc) {
    $str64 = strval($str64);
    $str_enc = strval($str_enc);
    if (empty($str64) && $str64 !== '0') {
        return false;
    }
    $str = strtr($str64, array('-' => '+', '_' => '/'));
    if ($str === FALSE) {
        return false;
    }
    $str = base64_decode($str);
    if ($str === FALSE) {
        return false;
    }
    $str_enc_len = strlen($str_enc);
    if ($str_enc_len < 1) {
        return $str;
    }
    $str_enc_len2 = 0 - $str_enc_len;
    $str_enc_2 = substr($str, $str_enc_len2);
    if ($str_enc_2 === $str_enc) {
        return substr($str, 0, $str_enc_len2);
    }
    return false;
}

/**
 * 文件下载
 * 传入 self_base64_encode($_GET['filepath']) 文件路径
 */
function downFile() {
    $filepath = self_base64_decode($_GET['filepath']);
    $filename = $_GET['filename'] ? $_GET['filename'] : basename($filepath);
    Header("Content-type: application/octet-stream");
    Header("Accept-Ranges: bytes");
    Header("Accept-Length: " . filesize($filepath));
    Header("Content-Disposition: attachment; filename=" . $filename);
    readfile($filepath);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值