2012-06-22 2 views
2

페이지에서 사용중인 Wordpress 테마 템플릿 파일이 있습니다. 템플릿은 db에 대해 배열을 쿼리 한 다음 csv 파일에 결과를 출력하려고 시도합니다. 브라우저에 출력되지 않습니다. 여기에 코드입니다 :php : // 결과가 빈 파일로 출력됩니다.

/*** 
* Output an array to a CSV file 
*/ 
function download_csv_results($results, $name = NULL) 
{ 
if(! $name) 
{ 
    $name = md5(uniqid() . microtime(TRUE) . mt_rand()). '.csv'; 
} 

header('Content-Type: text/csv'); 
header('Content-Disposition: attachment; filename='. $name); 
header('Pragma: no-cache'); 
header("Expires: 0"); 

$outstream = fopen("php://output", "w"); 

foreach($results as $result) 
{ 
    fputcsv($outstream, $result); 
} 
fclose($outstream); 
} 

파일은 예상대로 사용자의 다운로드 디렉토리에 기록하지만, 비어있다. 117 개 요소의 결과가 있는지 확인하기 위해 디버깅했습니다. 위의 루프가 실행 중입니다. 마치 출력 버퍼가 플러시되지 않거나 Wordpress에 의해 지워지는 것입니다.

+0

당신이'ob_'functions으로 시도 적이 있습니까? – Havelock

답변

1

당신이 사용하려고 수 :

$outstream = fopen("php://output", "a"); 

대신 :

$outstream = fopen("php://output", "w"); 
+0

감사합니다. Phenix. 불행히도 결과는 동일합니다. –

+0

fputcsv() 대신 fwrite()를 시도 했습니까? –

+0

감사. fwrite가 트릭을합니다. 배열 출력을 쉼표와 따옴표로 포맷해야했습니다. –

관련 문제