728x90

스마트폰 앱을 다운로드하는 코드이다.

파일 다운로드하는 코드에 스마트폰 장치를 인식하는 코드와 접목하여 어플을 다운로드 하도록 한다.

파일을 직접 접속하는 경우에는 동작하지 않도록 $_SERVER['HTTP_REFERER'] 기능을 사용하여 체크한다.

좀 더 정교하게 보완하는 것도 좋을 것이다.


<?php
require_once "deviceChk.php"; // 접속 Device 체크
if($mtype > 2) { // PC에서 접속하면 아래 코드 미 실행
    echo 'no access';
    exit;
}
require_once 'config/config.php';
if($mtype==2){
    $url='<a href="appDown.php">ABC <br/>어플 다운로드</a>';
}
?>

<!DOCTYPE html>
<html lang="ko">
<head>
  <title>ABC App 다운로드</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
    <div class="row align-items-center justify-content-center" style="height:60vh;">
        <div>
            <h4><?php echo $url;?></h4>
        </div>
    </div>
</div>
</body>
</html>

=== appDown.php ===

<?php
if(!isset($_SERVER['HTTP_REFERER'])) {
    echo 'direct access denied!';
    exit;
}

require_once "deviceChk.php"; // 접속 Device 체크
if($mtype==2){ // Android 폰 이면
    require_once 'config/config.php';
    $filepath = './files/'.$hostAbb.'.apk';
    $filesize = filesize($filepath);
    $path_parts = pathinfo($filepath);
    $filename = $path_parts['basename'];
    $extension = $path_parts['extension'];

    header("Pragma: public");
    header("Expires: 0");
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename='$filename'");
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: $filesize");
    header("Cache-Control: cache, must-revalidate");
    header("Pragma: no-cache");

    ob_clean();
    flush();
    //readfile($filepath); //Read and stream the file
    ///*
    $fp = fopen($filepath, "rb"); //rb 읽기전용 바이러니 타입
    if(!fpassthru($fp)) {
        fclose($fp);
    }
    //*/
}
?>


위 다운로드 코드에 약간 문제가 있는지 다운로드가 제대로 되지 않는 폰이 있어서 코드를 다시 수정했다.

아래 코드는 정상적으로 잘 다운로드 된다. 여러 폰에서 테스트를 했다. (update 2019.6.18)

<?php
if(!isset($_SERVER['HTTP_REFERER'])) {
    echo 'direct access denied!';
    exit;
}

require_once "deviceChk.php"; // 접속 Device 체크
if($mtype==2){ // Android 폰 이면
    require_once 'config/config.php';
    $filepath = './files/'.$hostAbb.'.apk';
    $filesize = filesize($filepath);
    $path_parts = pathinfo($filepath);
    $filename = $path_parts['basename'];
    $extension = $path_parts['extension'];

   header('Content-Description: File Transfer');
   header('Content-Type: application/octet-stream');
   header('Content-Disposition: attachment; filename="' . $filename . '"');
   header("Content-Transfer-Encoding: Binary");
   header('Expires: 0');
   header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
   header('Pragma: public');
   header("Content-length: ".filesize($filepath));
   ob_clean();
   flush();
   readfile($filepath);
}
?>


블로그 이미지

Link2Me

,