2016-10-31 3 views
1

후 나는 fputcsv을 가진 파일을 생성파일 빈 fputcsv

$handle = fopen('php://output', 'w+'); 

    // Add the header of the CSV file 
    fputcsv($handle, array('Name', 'Surname', 'Age', 'Sex'), ';'); 
    // Query data from database 

    // Add the data queried from database 
    foreach ($results as $result) { 
     fputcsv(
      $handle, // The file pointer 
      array(...), // The fields 
      ';' // The delimiter 
     ); 
    } 

    file_put_contents('mycsv.csv',fgetcsv($handle), FILE_APPEND); 

    fclose($handle); 
.... 

내가 mycsv.csv에 출력을 저장하고 싶지만 파일이

답변

1

php://output 비어는 echo 또는 print처럼 작동하는 stream입니다 . 즉, 표준 출력 (콘솔 또는 브라우저 일 가능성이 있음)으로 작성하고 있음을 의미합니다.

콘텐츠를 CSV 파일로 작성하려면 file_put_contents 대신에이 파일을 열거 나 PHP를 사용하여 직접 만들어보십시오.

$handle = fopen("mycsv.csv","wb"); 
fputcsv($handle, array('Name', 'Surname', 'Age', 'Sex'), ';'); 
//...put your code 
rewind($handle);//optional.it will set the file pointer to the begin of the file