2012-01-19 14 views
4

이름과 버전이 지정된 파일을 다운로드하는 PHP 스크립트를 만들고 있습니다. 파일은 다음과 같이 서버에 저장됩니다PHP로 이름이 변경된 파일 다운로드

/dl/Project1/1.0.txt 
/dl/Project1/1.1.txt 
/dl/Project2/2.3.jar 
/dl/Project2/2.3.1.jar 

그리고 경로는 파일과 같을 것이다 검색 : 실제로 파일을 다운로드 할 때

download.php?name=Project1&type=txt&version=1.0 
download.php?name=Project1&type=txt&version=1.1 
download.php?name=Project2&type=jar&version=2.3 
download.php?name=Project2&type=jar&version=2.3.1 

문제가 발생합니다. 이 예제에서는 Project1.txt로 다운로드 할 첫 두 파일과 Project2.jar로 다운로드 할 마지막 두 파일이 필요합니다. 이 기능을 사용하려면 임시로 이름을 어떻게 바꿀 수 있습니까?

답변

6

파일 이름을 정의하는 헤더를 전송합니다.

$filename = $name . "." . $type; 
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'); 
header('Pragma: public'); 
header('Content-Length: ' . filesize($file)); 

머리글을 추가했습니다. 머리글도 함께 보내야합니다. 이것은 see in the PHP documentation on readfile의 수정 된 예입니다. 당신은 아마 단순히 내용 - 처리 헤더 사용할

0

: 방금 헤더의 이름을 변경해야합니다 당신은 이름을 바꿀 필요가 없습니다

header('Content-disposition: attachment; filename=Project1.txt'); 
readfile('Project1/1.0.txt'); 
2

는 스크립트가 :

<?php 
// Check is set all params 
if (isset($_GET['name'], $_GET['type'], $_GET['version'])) { 
    // Get the params into variables. 

    // Secure replace to avoid the user downloading anyfile like @Kristian Antonsen said. 
    // Replace all '..' to a single '.'; 
    $name = preg_replace('/[\.]{2,}/', '.', trim($_GET['name'])); 
    // Replace any strange characters. 
    $type = preg_replace('/[^A-Za-z0-9]/', '', trim($_GET['type'])); 
    // Replace any letter and strange character. 
    $version = preg_replace('/[^0-9\.]/', '', trim($_GET['version'])); 

    /** 
    * Check is all the params filled with text 
    * and check if the version is in the right format. 
    */ 
    if (!empty($name) && 
     !empty($type) && 
     !empty($version) && 
     preg_match('/^[0-9](\.[0-9])+$', $version)) { 
    /** 
    * Get the file path, here we use 'dirname' to get the absolute path 
    * if the download.php is on root 
    */ 
    $filePath = dirname(__FILE__) . '/dl/' . $name . '/' . $version . '.' . $type; 

    // Check if the file exist. 
    if (file_exists($filePath)) { 
     // Add headers 
     header('Cache-Control: public'); 
     header('Content-Description: File Transfer'); 
     header('Content-Disposition: attachment; filename=' . $name . '.' . $type); 
     header('Content-Length: ' . filesize($filePath)); 
     // Read file 
     readfile($filePath); 
    } else { 
     die('File does not exist'); 
    } 
    } else { 
    die('Missing params'); 
    } 
} 
+0

맞습니다. 사용자가 anyfile을 다운로드 할 수있는 버그가 있습니다. 지금 바로 수정하려고합니다. –

+0

수정 됨. 나는'preg_replace'를 추가하여 장소에서 이해가되지 않는 문자를 대체하고 파일을 얻기 전에'preg_match' 검사를 추가했습니다. –

+0

예, 이제 해결하셨습니다. 일반적인 해결책은 분명히 나쁜 의도가 있기 때문에 아무에게나 접근하려고 시도하는 사람을 거부하는 것입니다. 따라서 아무 것도 제공하지 않아도됩니다. – kba

관련 문제