2012-01-09 4 views
0

정적 파일을 제공하는 drupal 모듈을 작성하고 싶지만 www.mydomain.com/moduleName/fileName 주소에서 제공하고 싶습니다. FileName은 아무거나 (예 : javascript/app.js 또는 photos/imageA.jpg). 가능한 경우 실제 위치로 파일을 복사하고 싶지 않습니다. 이상적으로 모듈이 두 위치를 핫 링크하기를 원합니다.정적 파일에 대해 drupal에서 custom_url_rewrite 사용

처럼 보이는 콜백 메뉴를 사용하려고했습니다. $ numargs = func_num_args(); $ arg_list = func_get_args();

$fileName = drupal_get_path('module', 'moduleName') . "/files"; 
for ($i = 0; $i < $numargs; $i++) { 
    $fileName = $fileName . "/" . $arg_list[$i]; 
} 
if ($numargs == 0) { 
    $fileName = drupal_get_path('module', 'moduleName') . "/files/index.html"; 
} 

if (file_exists($fileName)) { 
    header('Content-Type: ' . getFileMimeType($fileName)); 
    $handle = fopen($fileName, "r"); 
    $contents = fread($handle, filesize($fileName)); 
    fclose($handle); 
    echo $contents; 
} else { 
    drupal_json_output(array('exists' => file_exists($fileName), 'fileName' => $fileName)); 
} 
drupal_exit(); 

는 getFileMimeType는

function rcsarooms_getFileMimeType($file) { 
    $extension = pathinfo($file, PATHINFO_EXTENSION); 
    if ($extension == "html" || $extension == "md") { 
     $type = "text/html; charset=utf-8"; 
    } else if ($extension == "css") { 
     $type = "text/css"; 
    } else if ($extension == "js") { 
     $type = "application/x-javascript"; 
    } else if ($extension == "json") { 
     $type = "application/json"; 
    } else if ($extension == "ico") { 
     $type = "image/x-icon"; 
    } else if ($extension == "png") { 
     $type = "image/png"; 
    } else { 
     $type = "charset=UTF-8"; 
     echo $extension; 
     drupal_exit(); 
     return; 
    } 
    return $type; 
} 

과 같은 경우 그러나 이것은 어떤 텍스트 잘 작동하는 동안 파일은 (예를 들어, PNG, ICO 등) 나는 또한에 가진 마음에 들지 않는 바이너리 파일을 완전히 실패 그런 MIME 타입을 지정하십시오.

질문은 내가 할 일을 custom_url_rewrite가 할 수 있는지, 그렇다면 사용자 정의 URL 재 작성 기능에 무엇이 있어야하고 어디로 가야할까요?

나는이 시도했지만, 작동하지 않는 것 : 난 그냥 (작업을 수행 php.net에이 기능을 발견

function custom_url_rewrite($op, $result, $path) { 
    if (strpos($result, "data") === false) { 
     if ($op == 'alias') { 
      if (preg_match('|^' . drupal_get_path('module', 'myModule') . '/files/(.*)|', $path, $matches)) { 
       return 'myModule/' . $matches[1]; 
      } 
     } 

     if ($op == 'source') { 
      if (preg_match('|^myModule/(.*)|', $path, $matches)) { 
       return drupal_get_path('module', 'myModule') . '/files/'.$matches[1]; 
      } 
     } 
    } 

    return $result; 
} 

답변

1

난 여전히 관심이 있지만 다른 사람이 알고하기 당신은보다 간결 솔루션 (http://php.net/manual/en/function.fpassthru.php) fpassthru``] 시도 할 수 있습니다

function readfile_chunked($filename) { 
    $chunksize = 1 * (1024 * 1024); // how many bytes per chunk 
    $buffer = ''; 
    $cnt = 0; 
    $handle = fopen($filename, 'rb'); 
    if ($handle === false) { 
     return false; 
    } 
    ob_end_clean(); //added to fix ZIP file corruption 
    ob_start(); //added to fix ZIP file corruption 
    header('Content-Type:'); //added to fix file corruption 
    header('Cache-Control: max-age=7200'); //time in seconds. so cache for 2 hours 
    while (!feof($handle)) { 
     $buffer = fread($handle, $chunksize); 
     echo $buffer; 
     ob_flush(); 
     flush(); 
    } 
    fclose($handle); 
} 
+1

이 일을 더 나은 방법)을 가지고, 항상 작동 나를 위해. 아시다시피,''custom_url_rewrite()'] (http://api.drupal.org/api/drupal/developer--hooks--core.php/function/custom_url_rewrite/5)는 드루팔 (Drupal 5)에서만 사용되었습니다. 왜 그걸 사용해서 기쁨을 얻지 못했죠? – Clive

+1

이것이 정말로 오래된 게시물이라는 것을 알고 있지만 위의 코드가 조금 엉망이거나 반복적이라는 것을 다른 사람이 읽도록하고 싶었습니다. 다음은 정리 된 버전입니다. http://gist.github.com/3189322 – bokonic

+0

감사합니다. 항상 좋은 리팩터를 사랑합니다. 오래된 게시물이지만 코드는 아직 사용 중입니다. – ForbesLindesay

관련 문제