I'm currently writing some data to an SplFileObject
like this:
$fileObj = new SplFileObject('php://text/plain,', "w+");
foreach($data as $row) {
$fileObj->fputcsv($row);
}
Now, I want to dump the whole output (string) to a variable.
I know that SplFileObject::fgets
gets the output line by line (which requires a loop) but I want to get it in one go, ideally something like this:
$fileObj->rewind();
$output = $fileObj->fpassthru();
However, this does not work as it simply prints to standard output.
There's a solution for what I'm trying to achieve using stream_get_contents()
:
pass fpassthru contents to variable
However, that method requires you to have direct access to the file handle.
SplFileObject
hides the file handle in a private property and therefore not accessible.
Is there anything else I can try?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…