2011-09-22 16 views
5

나는 여기에 도움을 주신 덕분에 .php에 아주 간단한 Download Code redeemer를 만들었으며 다운로드를 제공하는 가장 좋은 방법은 무엇인지 알아 내려고 노력하고 있습니다. 유효성 검사가 성공적이면 기본적으로 -양식 제출 후 유효성 확인 후 제출

사용자가 잘못된 코드 입력 -> 페이지가 오류 메시지와 함께 새로 고침됩니다. 사용자가 유효한 코드 입력 -> 다운로드 '다른 이름으로 저장'-> 페이지 새로 고침.

분당 http://www.zubrag.com/scripts/download.php을 사용하고 있지만 일단 다운로드가 시작되면 내 양식은 페이지를 새로 고치지 만 콘텐츠를 절반 만로드합니까?!

이것은 내가 수행 한 PHP 스크립트의 양식입니다.

<div class="dcrForm"> 
    <p>Have a physical copy of this release? Claim your digital download by entering your Download Code below.</p> 
    <form action="index.php" method="post"> 
     <input type="text" name="code" class="dcrInput" value=""> 
     <input type="submit" name="harrisSubmit" class="dcrSubmit" value="Submit"> 
    </form> 
<?php 
    include("scripts/dcr_config.php"); 
    $code=""; 
    $log=""; 

    if (isset($_POST['harrisSubmit'])) 
    { 
     $code=$_POST['code']; 

     $link = mysql_connect($hostname, $dbusername, $dbpassword); 
     mysql_select_db("$databasename"); 

     $query = "select count from $harris where code='$code'"; 
     if ($q=mysql_query($query)) 
      if ($r=mysql_fetch_array($q)){ 
       if ($r[0]<3) 
       { 
        $subquery="update $tbname set count='".($r[0]+1)."' where code='$code'"; 
        mysql_query($subquery); 
        ?><script>window.location.href="download.php?f=test.txt";</script><?php 
       } 
      } 
     $log="<p>Invalid code. Try Again.</p>"; 
    } 
    echo $log.""; 
?> 
</div> 

다운로드를 제공하는 가장 좋은 방법은 누구에게 아이디어가 있습니까? 현재 파일 위치를 가진 사람은 파일을 다운로드 할 수 있지만 파일을 보호 할 수있는 방법이 무엇인지 잘 모르겠다.

답변

4

나는 지금까지 해왔다 니 다행이다!

사용자를 다운로드 스크립트로 리디렉션하려는 경우 해당 스크립트에는 승인되지 않은 다운로드를 방지하기 위해 일종의 토큰이 첨부되어 있어야합니다. 기본적으로 주어진 코드 또는 토큰을 다시 확인합니다.

<?php 

include "scripts/dcr_config.php"; 
$code = ""; 
$log = ""; 

if (isset($_POST['harrisSubmit'])) { 
    $code = trim($_POST['code']); 

    $link = mysql_connect ($hostname, $dbusername, $dbpassword); 
    mysql_select_db ("$databasename"); 

    $code = mysql_real_escape_string($code); // very important! protects against exploits 

    $query = "select count from $harris where code='$code'"; 
    if ($q = mysql_query ($query)) { 
     if ($r = mysql_fetch_array ($q)) { 
      if ($r [0] < 3) { 
       $subquery = "update $tbname set count='" . ($r [0] + 1) . "' where code='$code'"; 
       mysql_query ($subquery); 

       $file = '/path/to/protecteddownload.txt'; 

       // send file to browser as a download dialog 
       // no content can be output prior to these header() calls 
       header('Content-type: application/octet-stream'); 
       header('Content-Disposition: attachment; filename="file.txt"'); 
       header('Content-Length: ' . filesize($file)); 
       header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 
       header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); 

       echo file_get_contents($file); 
       exit; // terminate script 
      } else { 
       $log = 'Sorry, this code has already been redeemed.'; 
      } 
     } else { 
      $log = 'Invalid download code. Try again.'; 
     } 
    } else { 
     // query failed 
     $log = 'An error occurred validating your code, please try again later.'; 
    } 

    $log = "<p>Invalid code. Try Again.</p>"; 
} 

?> 

<?php if (isset($log) && $log != ''): ?> 
<strong class="error"><?php echo $log ?></strong> 
<?php endif; ?> 

<div class="dcrForm"> 
<p>Have a physical copy of this release? Claim your digital download by 
entering your Download Code below.</p> 
<form action="index.php" method="post"><input type="text" name="code" 
    class="dcrInput" value=""> <input type="submit" name="harrisSubmit" 
    class="dcrSubmit" value="Submit"></form> 
</div> 

다운로드 스크립트는 내가 위에서 무엇을 일부 아마도 유사하다 : 대신에 당신이 할 수있는 다운로드 스크립트로 리디렉션 자바 스크립트를 출력 위의 스크립트에서

. 이 예제에서 중요한 점은 file_get_contents로 제공하는 파일에 웹에서 액세스 할 수 없다는 것입니다. 유효한 코드가 입력 될 때만 보내십시오.

+0

안녕하세요, 덕분에, 그것은 매우 유용한 프로젝트였습니다. 위의 솔루션을 시도했지만 웹에서 액세스 할 수있는 위치에 $ 파일을 설정하고 내가 가리키는 파일 이름을 변경하더라도 다운로드가 제공되지 않고 페이지가 새로 고쳐지고 오직 절반 만로드됩니다! – Bantros

+0

절대 경로 또는 상대 경로를 사용하고 있습니까? – dizas

+0

헤더를 보내려는 경우, 특히 다운로드를 제공하는 경우 HTML 또는 공백을 출력하기 전에 출력 할 수 있기 때문에 코드의 구조가 조금 변경되었습니다. 예제에 표시되지 않은 헤더를 보내는 코드 블록 위에 다른 HTML이 있습니까? 그렇다면 다운로드를 제공하는 스크립트에 파일을 제공하기 전에 다른 출력이 없는지 확인하십시오. 전에 응답하지 않아 죄송합니다.받은 편지함 메시지를 놓친 것 같습니다. – drew010

1

1 개의 빠른 질문이 있습니다.이 파일의 크기는 얼마입니까? 브라우저에 파일을 읽는 동안 PHP 시간 초과가 발생하는 경우 일 수 있습니까?

PHP 설정으로이 문제를 해결할 수 있습니다 (http://php.net/manual/en/function.set-time-limit.php).

그냥 내 2 센트

+0

나는 그것을 생각하지 않았다. 약 250MB 크기의 .zip이다. – Bantros

관련 문제