php下载文件

php下载文件

  • php下载文件

代码块

public function downFile() {
        $filePath = '文件路径';
        $filename = basename($filePath);
        header("Content-type: application/octet-stream");
        //处理中文文件名
        $ua = $_SERVER["HTTP_USER_AGENT"];
        $encoded_filename = rawurlencode($filename);
        if (preg_match("/MSIE/", $ua)) {
            header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
        } else if (preg_match("/Firefox/", $ua)) {
            header("Content-Disposition: attachment; filename*=\"utf8''" . $filename . '"');
        } else {
            header('Content-Disposition: attachment; filename="' . $filename . '"');
        }

        header("Content-Length: ". filesize($filePath));
        readfile($filePath);
    }

你可能感兴趣的:(php开发那点事儿,php,php下载文件)