filename = Carbon::today()->toDateString().'-'.str_random('5').'-'.$name.'.xlsx'; $this->filename = $name.'.xlsx'; //$this->path = storage_path('exports/' . $this->filename); $this->path = $directory . '/' . $this->filename;// storage_path('exports/' . $this->filename); //$this->path = $path; $dirPath = dirname($this->path); // 确保目录存在 // if (!is_dir($dirPath)) { // mkdir($dirPath, 0777, true); // } if (is_dir($dirPath)) { $this->fd = fopen($this->path, 'w+'); if ($this->fd) { //add BOM to fix UTF-8 in Excel fputs($this->fd, $bom = (chr(0xEF) . chr(0xBB) . chr(0xBF))); /* $table = $data; foreach ($table as $fields) { fputcsv($fd, $fields); }*/ //fclose($fd); } } } public function putRow($row) { if (!$this->fd) { $this->fd = fopen($this->path, 'a+'); } return fputcsv($this->fd, $row); } public function closeFile() { if ($this->fd) { fclose($this->fd); $this->fd = null; } } }