2014-09-22 7 views
0

데이터베이스의 내용을 기반으로 CSV 파일을 만드는 다음 스크립트를 작성했습니다. 스크립트 자체는 완벽하게 작동하고 CSV 파일을 만들어 예상대로 채 웁니다. 문제는 파일을 자동으로 다운로드 할 때 파일이 비어 있지만 FTP를 통해 호스팅 서버에서 파일을 다운로드 할 때 정보로 채워진다는 것입니다.생성 된 CSV 다운로드가 비어 있지만 내용이 있음

파일이 성공적으로 작성되기 전에 파일이 너무 빨리 다운로드되고 있습니까? 이 문제를 해결하기 위해 할 수있는 일이 있습니까? 추가 테스트 후이

// Establish the MySQL Database Connection 
include_once("./include/database.php"); 
include("functions.php"); 

ob_start(); //start output buffering 

$filename = 'devices.csv'; 
$headers = array('ID', 'Device', 'Name', 'Type', 'Scope', 'OS', 'Datacenter'); 

$handle = fopen($filename, 'w'); 
fputcsv($handle, $headers, ',', '"'); 

$sql = mysql_query("SELECT * FROM devices ORDER BY name ASC", $dp_conn); 

while($results = mysql_fetch_object($sql)) 
{ 
    $type = getDeviceType($results->type, $dp_conn); 
    $scope = getDeviceScope($results->scope, $dp_conn); 
    $os = getOS($results->os, $dp_conn); 
    $datacenter = getDatacenter($results->datacenter, $dp_conn); 

    $row = array(
     $results->id, 
     $results->device_id, 
     $results->name, 
     $type, 
     $scope['name'], 
     $os, 
     $datacenter 
    ); 

    fputcsv($handle, $row, ',', '"'); 
} 

ob_end_clean(); //ending output buffering. 

// rewind the "file" with the csv lines 
fseek($handle, 0); 

header('Content-Type: application/csv'); 
header('Content-Disposition: attachment; filename="' . $filename . '";'); 

// make php send the generated csv lines to the browser 
fpassthru($handle); 

fclose($handle); 

답변

2

을 넣고 주제에 유사한 게시물을 발견

<?php 

    // Establish the MySQL Database Connection 
    include_once("./include/database.php"); 
    include("functions.php"); 

    $filename = 'devices.csv'; 
    $headers = array('ID', 'Device', 'Name', 'Type', 'Scope', 'OS', 'Datacenter'); 

    $handle = fopen($filename, 'w'); 
    fputcsv($handle, $headers, ',', '"'); 

    $sql = mysql_query("SELECT * FROM devices ORDER BY name ASC", $dp_conn); 

    while($results = mysql_fetch_object($sql)) 
    { 
     $type = getDeviceType($results->type, $dp_conn); 
     $scope = getDeviceScope($results->scope, $dp_conn); 
     $os = getOS($results->os, $dp_conn); 
     $datacenter = getDatacenter($results->datacenter, $dp_conn); 

     $row = array(
      $results->id, 
      $results->device_id, 
      $results->name, 
      $type, 
      $scope['name'], 
      $os, 
      $datacenter 
     ); 

     fputcsv($handle, $row, ',', '"'); 
    } 

    // rewind the "file" with the csv lines 
    fseek($handle, 0); 

    header('Content-Type: application/csv'); 
    header('Content-Disposition: attachment; filename="' . $filename . '";'); 

    // make php send the generated csv lines to the browser 
    fpassthru($handle); 

    fclose($handle); 

?> 
0

시도, 나는 수정을 발견했다. 파일에 fopen()을 사용하는 대신 데이터를 메모리에 썼다. 이제는 올바르게 작동한다.

<?php 

    // Establish the MySQL Database Connection 
    include_once("./include/database.php"); 
    include("functions.php"); 

    $filename = 'devices.csv'; 
    $headers = array('ID', 'Device', 'Name', 'Type', 'Scope', 'OS', 'Datacenter'); 

    //$handle = fopen($filename, 'w'); 
    $handle = fopen('php://memory', 'w'); 
    fputcsv($handle, $headers, ',', '"'); 

    $sql = mysql_query("SELECT * FROM devices ORDER BY name ASC", $dp_conn); 

    while($results = mysql_fetch_object($sql)) 
    { 
     $type = getDeviceType($results->type, $dp_conn); 
     $scope = getDeviceScope($results->scope, $dp_conn); 
     $os = getOS($results->os, $dp_conn); 
     $datacenter = getDatacenter($results->datacenter, $dp_conn); 

     $row = array(
      $results->id, 
      $results->device_id, 
      $results->name, 
      $type, 
      $scope['name'], 
      $os, 
      $datacenter 
     ); 

     fputcsv($handle, $row, ',', '"'); 
    } 

    // rewind the "file" with the csv lines 
    fseek($handle, 0); 

    header('Content-Type: application/csv'); 
    header('Content-Disposition: attachment; filename="' . $filename . '";'); 

    // make php send the generated csv lines to the browser 
    fpassthru($handle); 

    fclose($handle); 

?> 
+0

$ 핸들이 닫혀 있으면 작동하지 않습니다. 경고 : fpassthru() : 8은 44 번째 줄에있는 /home/devpo/public_html/device_csv.php의 유효한 스트림 리소스가 아닙니다. –

+0

이것은 출력 버퍼링의 경우 일 수 있습니다. 따라서 ob_start(); while 루프가 끝나고 ob_end_clean()이 끝나면 ... 그러면 모든 변경 사항이 파일로 푸시됩니다. – justrohu