webCOMAND

\io_comand_file\stream_writer

Class used to write output to a:

  • String - Write to a string in memory.  Uses fopen( 'memfile://key'. 'w').
  • Standard Output - Echo to STDOUT as it is written.  Uses fopen('php://output', 'w').
  • File - For a given filename.
  • Temporary File - Uses and fopen(tempname(), 'w')
  • Browser - Like Standard Output, except a filename and content disposition can be specified.
  • Stream - Write to a stream, such as a zip or tar file.

Example

$data = [['A', 'B', 'C'], [1, 2, 3]];

// write data to a CSV string in memory
$sw = new \io_comand_file\stream_writer();
$sw->open_string();
foreach($data as $row) {
    $sw->write_csv($row);
}
$csv = $sw->close();

// write the CSV string to a file compressed in a ZIP
$zw = new \io_comand_file\zip\zip_writer();
$zw->open_file('archive.zip');
$zw->add_from_string('data.csv', $csv);
$zw->close();

// write data to the screen (aka standard output)
$sw = new \io_comand_file\stream_writer();
$sw->open_stdout();
foreach($data as $row) {
    $sw->write_csv($row);
}
$sw->close();