데이터베이스의 내용을 기반으로 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);
$ 핸들이 닫혀 있으면 작동하지 않습니다. 경고 : fpassthru() : 8은 44 번째 줄에있는 /home/devpo/public_html/device_csv.php의 유효한 스트림 리소스가 아닙니다. –
이것은 출력 버퍼링의 경우 일 수 있습니다. 따라서 ob_start(); while 루프가 끝나고 ob_end_clean()이 끝나면 ... 그러면 모든 변경 사항이 파일로 푸시됩니다. – justrohu