2012-05-23 4 views
0

아래 코드를 사용하여 서버에서 mp3 파일을 다운로드하고 있습니다. 코드가 로컬 시스템 (Windows OS)에서 정상적으로 작동합니다.PHP 파일 다운로드가 Linux에서 작동하지 않습니다.

그러나 코드를 서버 (Linux)로 옮길 때 파일을 찾을 수 없습니다. 오류가 발생합니다. 나는 파일 경로가 올바른지 및 파일 여기

if ($fd = fopen ($filePath, "r")) { 
    $fsize = filesize($filePath); 
    $path_parts = pathinfo($filePath); 
    $ext = strtolower($path_parts["extension"]); 
    switch ($ext) {     
     case "mp3":     
     header("Content-type: audio/mpeg"); // add here more headers for diff. extensions 
     header("Content-Disposition: attachment; filename=\"".$originalFileName."\""); // use 'attachment' to force a download 
     break; 
     default; 
     header("Content-type: application/octet-stream"); 
     header("Content-Disposition: filename=\"".$originalFileName."\""); 
    } 
    header("Content-length: $fsize"); 
    header("Cache-control: private"); //use this to open files directly 
    while(!feof($fd)) { 
     $buffer = fread($fd, 2048); 
     echo $buffer; 
    } 
} 
fclose ($fd) 
+1

나는이 같은 것을 겪었을하고 문제가있는 파일이었다 이름이 잘못 되었다면 리눅스는 파일 확장자로 대소 문자를 구별합니다. 그래서'bla.JPG' 파일과'bla.jpg' 파일이 완전히 다른 것입니다. 이것을 확인하십시오 – Gerep

+0

어디서 오류가 발생합니까? fopen은 실패 했습니까? – xdazz

+1

또한 파일 사용 권한에 몇 가지 문제가있을 수 있습니다. 어떤 사용자 httpd가 실행되고 어떤 pemissions가 할당되는지/파일을 다운로드하려는 사용자에게 확인하십시오. – heximal

답변

0

내가 그 코드를 작성합니다 어떻게 읽을 수 있는지 확신 :

// A couple of sanity checks on the file path, return a meaningful error message for debugging 
if (!file_exists($filePath)) { 
    header('HTTP/1.1 404 Not Found'); 
    exit('The requested file was not found'); 
} else if (!is_readable($filePath)) { 
    header('HTTP/1.1 403 Forbidden'); 
    exit('The requested file is not accessible'); 
} 

// Get the content type from the extension 
// Consider using finfo instead: http://php.net/manual/en/ref.fileinfo.php 
$contentTypes = array(
    'mp3' => 'audio/mpeg', 
    'jpg' => 'image/jpeg', 
    'gif' => 'image/gif' 
    // etc etc 
); 
$ext = strtolower(pathinfo($filePath, PATHINFO_EXTENSION)); 
$contentType = (isset($contentTypes[$ext])) ? $contentTypes[$ext] : 'application/octet-stream'; 

// Content-* headers 
header("Content-Length: ".filesize($filePath)); 
header("Content-Type: {$contentType}"); 
// You will always want the same Content-Disposition: header, regardless of file type 
// The only time you would need a different one is if you want to serve "inline" content 
header("Content-Disposition: attachment; filename=\"{$originalFileName}\""); 

// No-Cache headers 
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past 

// We don't want the script to time out on large files 
set_time_limit(0); 

// Output the file 
readfile($filePath); 
exit; 
+0

안녕하세요 데이브, 위의 코드는 또한 항상 작동하지 않습니다. 또한 원래 질문에 붙여 넣은 같은 코드 동작합니다. 이상한 생각은 때로는 일부 파일 다운로드가 작동하고 일부는 작동하지 않습니다. 파일 크기 제한과 관련이 있습니까? 내 용량은 6MB입니다. – codlib

+0

어떤 방식으로 작동하지 않습니까? 실패하면 어떻게됩니까? – DaveRandom

+0

ok 어디서 실패했는지 알 수 없습니다. 하지만 아래는 Firefox에서 표시되는 오류 메시지입니다. ------------------------------------ --------------------- 파일을 찾을 수 없음 Firefox는 http://site.com/dm/d/o/에서 파일을 찾을 수 없습니다. dd6146c0f2c9b9e2a424a40cfa50d362/oi/yJY/JWiuvWN0jeRZVsAR00p/4d Xang3A67r9ffuUO8g =/auth/900f12126fc0efcc896b6ae7c829c91f /. 대문자 또는 기타 입력 오류의 파일 이름을 확인하십시오. 파일의 이동, 이름 변경 또는 삭제 여부를 확인하십시오. ------------------------------------------------ ------- – codlib

관련 문제