Skip to content

Commit 5cd3410

Browse files
committed
CSV writer also use common code to open file
1 parent 081b49f commit 5cd3410

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

src/PhpSpreadsheet/Writer/BaseWriter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,17 @@ public function openFileHandle($filename): void
108108
return;
109109
}
110110

111-
$fileHandle = fopen($filename, 'wb+');
111+
$fileHandle = $filename ? fopen($filename, 'wb+') : false;
112112
if ($fileHandle === false) {
113-
throw new Exception('Could not open file ' . $filename . ' for writing.');
113+
throw new Exception('Could not open file "' . $filename . '" for writing.');
114114
}
115115

116116
$this->fileHandle = $fileHandle;
117117
$this->shouldCloseFile = true;
118118
}
119119

120120
/**
121-
* Close file handle only we opened it ourselves.
121+
* Close file handle only if we opened it ourselves.
122122
*/
123123
protected function maybeCloseFileHandle(): void
124124
{

src/PhpSpreadsheet/Writer/Csv.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
66
use PhpOffice\PhpSpreadsheet\Spreadsheet;
7-
use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;
87

98
class Csv extends BaseWriter
109
{
@@ -91,17 +90,7 @@ public function save($pFilename)
9190
Calculation::setArrayReturnType(Calculation::RETURN_ARRAY_AS_VALUE);
9291

9392
// Open file
94-
if (is_resource($pFilename)) {
95-
$fileHandle = $pFilename;
96-
} elseif (!$pFilename) {
97-
$fileHandle = false;
98-
} else {
99-
$fileHandle = fopen($pFilename, 'wb+');
100-
}
101-
102-
if ($fileHandle === false) {
103-
throw new WriterException("Could not open file $pFilename for writing.");
104-
}
93+
$this->openFileHandle($pFilename);
10594

10695
if ($this->excelCompatibility) {
10796
$this->setUseBOM(true); // Enforce UTF-8 BOM Header
@@ -110,13 +99,15 @@ public function save($pFilename)
11099
$this->setDelimiter(';'); // Set delimiter to a semi-colon
111100
$this->setLineEnding("\r\n");
112101
}
102+
113103
if ($this->useBOM) {
114104
// Write the UTF-8 BOM code if required
115-
fwrite($fileHandle, "\xEF\xBB\xBF");
105+
fwrite($this->fileHandle, "\xEF\xBB\xBF");
116106
}
107+
117108
if ($this->includeSeparatorLine) {
118109
// Write the separator line if required
119-
fwrite($fileHandle, 'sep=' . $this->getDelimiter() . $this->lineEnding);
110+
fwrite($this->fileHandle, 'sep=' . $this->getDelimiter() . $this->lineEnding);
120111
}
121112

122113
// Identify the range that we need to extract from the worksheet
@@ -128,9 +119,10 @@ public function save($pFilename)
128119
// Convert the row to an array...
129120
$cellsArray = $sheet->rangeToArray('A' . $row . ':' . $maxCol . $row, '', $this->preCalculateFormulas);
130121
// ... and write to the file
131-
$this->writeLine($fileHandle, $cellsArray[0]);
122+
$this->writeLine($this->fileHandle, $cellsArray[0]);
132123
}
133124

125+
$this->maybeCloseFileHandle();
134126
Calculation::setArrayReturnType($saveArrayReturnType);
135127
Calculation::getInstance($this->spreadsheet)->getDebugLog()->setWriteDebugLog($saveDebugLog);
136128
}

0 commit comments

Comments
 (0)