테스트 사이트 - 개발 중인 베타 버전입니다

모바일에서 첨부파일 다운로드 안되거나 다운로드 된것이 정상적으로 안열릴때

· 7년 전 · 10449 · 2

제 서버 환경에서만 그럴 수 있겠습니다만, PC에서는 첨부파일이 잘 다운로드 되었지만,

저는 이상하게 모바일에서는 pdf 파일을 다운로드 하면 

 

모바일 - 크롬에서는 다운로드 되지만 파일 안열림.

모바일 - 파폭에서는 다운로드 자체가 안됨.

 

이었습니다. 그래서 구글링 후 삽질 끝에 작성한 코드 공유 합니다.

이걸 하니, 다 정상인것 같은데, 역시 모바일 파폭에서는 download.php 라는 이름으로만

다운로드가 되고, pdf 뷰어에서 잘 열리기는 하네요..

 

파폭에서도 잘될 수 있도록 개선점 발견하시는 분 계시면 댓글 부탁드리면서,

저는 귀차니즘으로 파폭은 무시 ㅎㅎㅎ 하렵니다.

 

-------------------

 

/bbs/download.php 에서 95행 이하 주석 처리 후 다음 코드를 붙여넣으면 됩니다.

 

function output_file($file, $name, $mime_type='')
{
    if(!is_readable($file)) die('File not found or inaccessible!');
    $size = filesize($file);
    $name = rawurldecode($name);
    $known_mime_types=array(
        "htm" => "text/html",
        "exe" => "application/octet-stream",
        "zip" => "application/zip",
        "doc" => "application/msword",
        "jpg" => "image/jpg",
        "php" => "text/plain",
        "xls" => "application/vnd.ms-excel",
        "ppt" => "application/vnd.ms-powerpoint",
        "gif" => "image/gif",
        "pdf" => "application/pdf",
        "txt" => "text/plain",
        "html"=> "text/html",
        "png" => "image/png",
        "jpeg"=> "image/jpg",
        "hwp"=> "application/x-hwp",
        "docx"=> "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
        "xlsx"=> "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        "pptx"=> "application/vnd.openxmlformats-officedocument.presentationml.presentation"
    );

    if($mime_type==''){
        $file_extension = strtolower(substr(strrchr($file,"."),1));
        if(array_key_exists($file_extension, $known_mime_types)){
            $mime_type=$known_mime_types[$file_extension];
        } else {
            $mime_type="application/force-download";
        };
    };
    @ob_end_clean();
    if(ini_get('zlib.output_compression'))
    ini_set('zlib.output_compression', 'Off');
    header('Content-Type: ' . $mime_type);
    header('Content-Disposition: attachment; filename="'.$name.'"');
    header("Content-Transfer-Encoding: binary");
    header('Accept-Ranges: bytes');

    if(isset($_SERVER['HTTP_RANGE']))
    {
        list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2);
        list($range) = explode(",",$range,2);
        list($range, $range_end) = explode("-", $range);
        $range=intval($range);
        if(!$range_end) {
            $range_end=$size-1;
        } else {
            $range_end=intval($range_end);
        }

        $new_length = $range_end-$range+1;
        header("HTTP/1.1 206 Partial Content");
        header("Content-Length: $new_length");
        header("Content-Range: bytes $range-$range_end/$size");
    } else {
        $new_length=$size;
        header("Content-Length: ".$size);
    }

    $chunksize = 1*(1024*1024);
    $bytes_send = 0;
    if ($file = fopen($file, 'r'))
    {
        if(isset($_SERVER['HTTP_RANGE']))
        fseek($file, $range);

        while(!feof($file) &&
            (!connection_aborted()) &&
            ($bytes_send<$new_length)
        )
        {
            $buffer = fread($file, $chunksize);
            echo($buffer);
            flush();
            $bytes_send += strlen($buffer);
        }
        fclose($file);
    } else
        die('Error - can not open file.');
    die();
}
set_time_limit(0);


$tmp = strpos(strrev($original), '.');
$temp = strlen($original) - $tmp;
if($tmp)
    $strExt = substr($file_name, strlen($strName) + 1, strlen($file_name));
else
    $strExt = ''; //확장자 없음
$strExt = strtolower($strExt);

output_file($filepath, $original, $strExt);
 

 

 

댓글 작성

댓글을 작성하시려면 로그인이 필요합니다.

로그인하기

댓글 2개

모바일 사파리도 될까요?
아이폰 확인결과 바로 열립니다.

게시글 목록

번호 제목
24318
24317
24315
24309
24294
24293
24277
24262
24260
24253
24251
24236
24233
24228
24226
24221
24214
24203
24201
24199
24196
24195
24194
24192
24191
24187
24185
24183
24172
24168