2011-12-06 3 views
3

이 코드를 다운로드 단추로 다운로드하려면 다운로드하십시오.다운로드 이미지 링크를 사용하여 PHP

<?  
    $filename = $_GET["filename"]; 
    $buffer = file_get_contents($filename); 

    /* Force download dialog... */ 
    header("Content-Type: application/force-download"); 
    header("Content-Type: application/octet-stream"); 
    header("Content-Type: application/download"); 

    /* Don't allow caching... */ 
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 

    /* Set data type, size and filename */ 
    header("Content-Type: application/octet-stream"); 
    header("Content-Transfer-Encoding: binary"); 
    header("Content-Length: " . strlen($buffer)); 
    header("Content-Disposition: attachment; filename=$filename"); 

    /* Send our file... */ 
    echo $buffer; 
?> 

것은이 파일의 이름은 예를 들어, 파일 이름에 전체 경로로 끝나는,이 코드입니다 :

<a href="download.php?filename=images/something.jpg"> 

라는 이미지 "images_something.jpg와 함께 종료 "

"images_ "을 최종 파일 이름에서 제거하고 싶습니다. 지금까지 행운이 없었습니다.

도움 주셔서 감사합니다. 당신은 폴더 이름이없는 파일 이름 부분을해야하는 경우

+1

그 스크립트를 사용하면 누구나 서버의 파일을 다운로드 할 수 있으므로 사용하기 전에 보안을 조사 할 것입니다. – JJJ

+0

나는 그 사실을 알지 못했다. 나를 보안 할 방법을 연구 할 수있는 웹 사이트를 가르쳐 주시겠습니까? –

답변

2
를 사용할 필요가

basename()

$filename = basename($path); 

PS

설정 Content-Type을 여러 번 할 수있다 다운로드를 강제하는 가장 좋은 방법은 아닙니다. 또한 희망 당신은 file_get_contents를 사용하기 전에 당신이 그 $filename 인수를 위생 처리하고 있습니다.

p.p.s

사용 readfile, 메모리에 캐시하지 않습니다.

2
$filename = basename($filename); 
header("Content-Disposition: attachment; filename=$filename"); 

파일 이름을 기본 이름만으로 설정 하시겠습니까?

변수를 변경하지 않으면 맨 위에 표시하지 마십시오. 따라서 변수를 변경하더라도 여전히 작동합니다.

관련 문제