PHP & Others

다운로드 소스

페이지 정보

본문

안녕하세요...
공간 입니다.. ^^;
아래에 다운로드 소스 팁을 올렸었는데...
게시판에서나 다른곳에서 그냥 down.php?no=1
이런씩으로 번호만 넘겨서 다운로드 하는 소스를 올려 봅니다...
많이 알고 있는 소스고 허접 하지만.... 올려봅니다...
저도 왕초보 시절이 있었던지라... (지금도 왕초보지만.. ^^;)
많은 덕담? 부탁드립니다.. ^^;;;
그럼....

<?
session_start();

  #down.php 를 새로운 창으로 띄움 (popup 등)
 
  # 설정 파일 읽어들임 dbconnection 등
  include "../lib/setup.php";

  # 다운로드 회수 증가나 다른부분 넣음
 
  # 아래부터 다운로드 부분
  # file1 = 업로드시 올린 파일명 (test.zip)
  # sfile1 = 실제 서버에 들어가 있는 파일저장 장소 (directory 포함 - /home/test/www/data/test.zip)
  $query="select no,file1,sfile1 from $board where no=$no";

  $result=mysql_query($query);

  if($result) {
    $total=mysql_num_rows($result);

    # 파일 정보가 DB 에 있는지 체크
    if($total) {
      $row=mysql_fetch_array($result);

      $file=$row[sfile1]; # 서버에 저장된 파일 정보
      $dnfile=$row[file1]; # 사용자가 받아갈 파일명
    } else {
      echo "
            <script>
            window.alert('저장할 파일에 문제가 발생하였습니다.');
            history.back();
            </script>
          ";
      exit;
    }
  } else {
    echo "
          <script>
          window.alert('DB Error');
          history.back();
          </script>
        ";
    # echo mysql_error();
    exit;
  }

$dn = "1"; // 1 이면 다운 0 이면 브라우져가 인식하면 화면에 출력
$dn_yn = ($dn) ? "attachment" : "inline";
$bin_txt = "1";
$bin_txt = ($bin_txt) ? "r" : "rb";

if(eregi("(MSIE 5.0|MSIE 5.1|MSIE 5.5|MSIE 6.0)", $HTTP_USER_AGENT))
{
  if(strstr($HTTP_USER_AGENT, "MSIE 5.5"))
  {
    header("Content-Type: doesn/matter");
    Header("Content-Length: ".(string)(filesize("$file")));
    Header("Content-Disposition: filename=$dnfile"); 
    Header("Content-Transfer-Encoding: binary"); 
    Header("Pragma: no-cache"); 
    Header("Expires: 0"); 

  }

  if(strstr($HTTP_USER_AGENT, "MSIE 5.0"))
  {
    Header("Content-type: file/unknown");
    header("Content-Disposition: attachment; filename=$dnfile");
    Header("Content-Description: PHP3 Generated Data");
    header("Pragma: no-cache");
    header("Expires: 0");
  }

  if(strstr($HTTP_USER_AGENT, "MSIE 5.1"))
  {
    Header("Content-type: file/unknown");
    header("Content-Disposition: attachment; filename=$dnfile");
    Header("Content-Description: PHP3 Generated Data");
    header("Pragma: no-cache");
    header("Expires: 0");
  }
 
  if(strstr($HTTP_USER_AGENT, "MSIE 6.0"))
  {
    Header("Content-type: application/x-msdownload");
    Header("Content-Length: ".(string)(filesize("$file")));  // 이부부을 넣어 주어야지 다운로드 진행 상태가 표시
    Header("Content-Disposition: attachment; filename=$dnfile"); 
    Header("Content-Transfer-Encoding: binary"); 
    Header("Pragma: no-cache"); 
    Header("Expires: 0"); 
  }
} else {
  Header("Content-type: file/unknown");   
  Header("Content-Length: ".(string)(filesize("$file")));
  Header("Content-Disposition: $dn_yn; filename=$dnfile");
  Header("Content-Description: PHP3 Generated Data");
  Header("Pragma: no-cache");
  Header("Expires: 0");
}

if (is_file("$file")) {
  $fp = fopen("$file", "rb");

  if (!fpassthru($fp))
    fclose($fp);

} else {
  echo "해당 파일이나 경로가 존재하지 않습니다.";
}

?>

관련자료

nuno님의 댓글

  보통 익스용 다운로드 프로그램 제작시에는
Content-Disposition: inline; filename=파일이름
을 사용합니다
attachment 를 사용시에는 다운이 다 끝났는데도 마우스에 모래시계가 계속 남는다는
겁니다. inline 을 쓰면 그런현상은 없어지더군요(음 -_-)
또한 여러가지 이점이 있죠~
download.php?filename=이쁘네.zip 요런게 다운로드 박스창에 뜨지만
inline 사용시 이쁘네.zip 으로 깔끔하게 나온다는 -_-;;
Today's proverb
“무릇 물이란 지세를 따라 흐르되 작은 틈도 놓치지 않고 적시니 지혜를 갖춘 자와 같고, 움직이면서 아래로 흘러가니 예를 갖춘 자와 같으며, 어떤 깊은 곳도 머뭇거리지 않고 들어가니 용기를 가진 자와 같고 장애물이 막혀서 갇히면 고요히 맑아지니 천명을 아는 자와 같으며, 험한 곳을 거쳐 멀리 흐르지만 끝내 남을 허물어뜨리는 법이 없으니 덕을 가진 자와 같다. 천지는 이것으로 이루어지고, 만물은 이것으로 살아가며, 나라는 이것으로 안녕을 얻고, 만사는 이것으로 평안해지며, 만물은 이것으로 바르게 되는 것이다. 이것이 지혜로운 자가 물을 좋아하는 이유이다.” (한영, <<한시외전>>)