>  기사  >  php教程  >  파일 요약 다운로드

파일 요약 다운로드

WBOY
WBOY원래의
2016-08-25 10:19:461303검색

HTTP 헤더를 생성하여 클라이언트에 데이터를 강제로 다운로드합니다. 이는 파일 다운로드를 구현할 때 유용합니다.
우선 @yangweijie의 작업 덕분에 php_fileinfo.dll 확장자를 열 필요가 없는 메서드가 파생되었습니다.
방법 1: 확장 프로그램 활성화//$file 文件路径<br> public function download_file($file){<br>         if(is_file($file)){<br>             $length = filesize($file);<br>             $type = mime_content_type($file);<br>             $showname =  ltrim(strrchr($file,'/'),'/');<br>             header("Content-Description: File Transfer");<br>             header('Content-type: ' . $type);<br>             header('Content-Length:' . $length);<br>             if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { //for IE<br>                 header('Content-Disposition: attachment; filename="' . rawurlencode($showname) . '"');<br>             } else {<br>                 header('Content-Disposition: attachment; filename="' . $showname . '"');<br>             }<br>             readfile($file);<br>         } else {<br>             $this->error('源文件不存在!');<br>         }<br>     }//$file 파일 경로 공개 함수 download_file($file){<br> If(is_file($file)){<br>                $length = 파일 크기($file);<br>              $type = mime_content_type($file);<br>                $showname = ltrim(strrchr($file,'/'),'/');<br> header("콘텐츠 설명: 파일 전송");<br>              header('콘텐츠 유형: ' . $type);<br> header('콘텐츠 길이:' . $length);<br> If (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { //IE의 경우<br> header('Content-Disposition: attachment; filename="' . rawurlencode($showname) . '"');<br>                                                      그 외 {<br> header('콘텐츠 처리: attachment; filename="' . $showname . '"');<br>             }<br>                파일 읽기($file);<br>            } else {<br>                $this->error('소스 파일이 존재하지 않습니다!');<br>        }<br> } }방법 2, php_fileinfo.dll이 켜져 있지 않으면 누가 우리를 서버 권한 없이 프로그래머로 만들고 다른 사람들의 업신여김을 받을 수 없게 됩니까? 아마도 당신은 이해할 것입니다.
공개 함수 force_download($filename)
{
If ($filename == ''){
>        }
If (FALSE === strpos($filename, '.')){
>        }
         $x = 폭발('.', $filename);         $extension = end($x);          $mimes =$this->getMimes(); ​​​​//찾을 수 없는 경우 기본 MIME을 설정하세요             if ( !isset($mimes[$extension])){              $mime = 'application/octet-stream';           }그 외{ $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];        }             // 서버 헤더 생성 If (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE") !== FALSE)            { header('콘텐츠 유형: "'.$mime.'"');                 header('Content-Disposition: attachment; filename="'.$filename.'"'); header('만료일: 0');            header('Cache-Control: 반드시 재검증, 사후 확인=0, 사전 확인=0');
            header("콘텐츠 전송-인코딩: 바이너리");
            header('Pragma: public');
            header("콘텐츠 길이: ".filesize($filename));
        }
        그 외
        {
            header('Content-Type: "'.$mime.'"');
            header('Content-Disposition: attachment; filename="'.$filename.'"');
            header("콘텐츠 전송-인코딩: 바이너리");
            header('만료: 0');
            header('Pragma: no-cache');
            header("콘텐츠 길이: ".filesize($filename));
        }
        파일 읽기($filename);
    }
비공개 함수 getMimes(){
        return $mimes = array(    'hqx'    =>    'application/mac-binhex40',
            'cpt'    =>    'application/mac-compactpro',
            'csv'    =>    array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel'),
            '빈'    =>    '애플리케이션/맥바이너리',
            'DM'    =>    '응용 프로그램/옥텟 스트림',
            '라하'    =>    '응용 프로그램/옥텟 스트림',
            'lzh'    =>    '애플리케이션/옥텟 스트림',
            'exe'    =>    array('application/octet-stream', 'application/x-msdownload'),
            '수업'    =>    '응용 프로그램/옥텟 스트림',
            'psd'    =>    'application/x-photoshop',
            '그래서'    =>    '응용 프로그램/옥텟 스트림',
            '바다'    =>    '응용 프로그램/옥텟 스트림',
            'dll'    =>    '애플리케이션/옥텟 스트림',
            '오다'    =>    '어플리케이션/오다',
            'pdf'    =>    array('application/pdf', 'application/x-download'),
            '아이'    =>    '신청서/포스트스크립트',
            'eps'    =>    '신청서/포스트스크립트',
            'ps'    =>    '신청서/포스트스크립트',
            'smi'    =>    '신청/smil',
            '미소'    =>    '신청/smil',
            '미프'    =>    'application/vnd.mif',
            'xls'    =>    array('application/excel', 'application/vnd.ms-excel', 'application/msexcel'),
            'ppt'    =>    array('application/powerpoint', 'application/vnd.ms-powerpoint'),
            'wbxml'    =>    '응용프로그램/wbxml',
            'wmlc'    =>    '응용프로그램/wmlc',
            'dcr'    =>    'application/x-director',
            '디렉터리'    =>    'application/x-director',
            'dxr'    =>    'application/x-director',
            'dvi'    =>    '응용프로그램/x-dvi',
            'gtar'    =>    'application/x-gtar',
            'gz'    =>    'application/x-gzip',
            'php'    =>    'application/x-httpd-php',
            'php4'    =>    'application/x-httpd-php',
            'php3'    =>    'application/x-httpd-php',
            'phtml'    =>    'application/x-httpd-php',
            'phps'    =>    'application/x-httpd-php-source',
            'js'    =>    'application/x-javascript',
            'swf'    =>    'application/x-shockwave-flash',
            '앉아'    =>    'application/x-stuffit',
            '타르'    =>    'application/x-tar',
            'tgz'    =>    array('application/x-tar', 'application/x-gzip-compressed'),
            'xhtml'    =>    'application/xhtml+xml',
            'xht'    =>    'application/xhtml+xml',
            'zip'    =>  array('application/x-zip', 'application/zip', 'application/x-zip-compressed'),
            '중간'    =>    '오디오/미디',
            '미디'    =>    '오디오/미디',
            'mpga'    =>    '오디오/mpeg',
            'mp2'    =>    '오디오/mpeg',
            'mp3'    =>    array('오디오/mpeg', '오디오/mpg', '오디오/mpeg3', '오디오/mp3'),
            'aif'    =>    '오디오/x-aiff',
            'aiff'    =>    '오디오/x-aiff',
            'aifc'    =>    '오디오/x-aiff',
            '램'    =>    '오디오/x-pn-realaudio',
            'rm'    =>    '오디오/x-pn-realaudio',
            'rpm'    =>    '오디오/x-pn-realaudio-플러그인',
            '라'    =>    '오디오/x-realaudio',
            'rv'    =>    'video/vnd.rn-realvideo',
            'wav'    =>    array('audio/x-wav', 'audio/wave', 'audio/wav'),
            'bmp'    =>    array('이미지/bmp', '이미지/x-windows-bmp'),
            'gif'    =>    '이미지/gif',
            'jpeg'    =>    array('image/jpeg', 'image/pjpeg'),
            'jpg'    =>    array('image/jpeg', 'image/pjpeg'),
            'jpe'    =>    array('image/jpeg', 'image/pjpeg'),
            'png'    =>    array('이미지/png',  '이미지/x-png'),
            '티파니'    =>    '이미지/tiff',
            '티프'    =>    '이미지/tiff',
            'css'    =>    '텍스트/CSS',
            'html'    =>    '텍스트/html',
            'htm'    =>    '텍스트/html',
            'shtml'    =>    '텍스트/html',
            'txt'    =>    '텍스트/일반',
            '텍스트'    =>    '텍스트/일반',
            '로그'    =>    array('text/plain', 'text/x-log'),
            'rtx'    =>    '텍스트/서식 있는 텍스트',
            'rtf'    =>    '텍스트/rtf',
            'xml'    =>    '텍스트/xml',
            'xsl'    =>    '텍스트/xml',
            'mpeg'    =>    '동영상/mpeg',
            'mpg'    =>    '동영상/mpeg',
            'mpe'    =>    '동영상/mpeg',
            'qt'    =>    '동영상/퀵타임',
            'mov'    =>    '동영상/퀵타임',
            'avi'    =>    '동영상/x-ms동영상',
            '영화'    =>    '동영상/x-sgi-movie',
            '문서'    =>    '응용프로그램/msword',
            'docx'    =>    array('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip'),
            'xlsx'    =>    array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip'),
            '단어'    =>    array('application/msword', 'application/octet-stream'),
            'xl'    =>    '응용프로그램/엑셀',
            'eml'    =>    '메시지/rfc822',
            'json' => array('application/json', 'text/json')
);이것을 보면 접미사가 파일 형식을 일치시키는 데 사용된다는 것을 모두가 이해하게 될 것입니다. mime_content_type의 기능을 구현합니다.
【광고】
쇼핑몰, 사무실, 기업 홈페이지, 유통 및 기타 시스템의 개발 및 2차 개발을 담당하고 있습니다. 상담을 환영합니다.
771831851

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.