有必要扩展“ sendContent ”的方法,即添加一种下载文件后删除目录的方法( deleteDirAfterSend )。
类: Symfony \ Component \ HttpFoundation \ BinaryFileResponse;
修改方法: sendContent();
如何正确实施?
protected $deleteDir = null;
/**
* Sends the file.
*
* {@inheritdoc}
*/
public function sendContent()
{
if (!$this->isSuccessful()) {
return parent::sendContent();
}
if (0 === $this->maxlen) {
return $this;
}
$out = fopen('php://output', 'wb');
$file = fopen($this->file->getPathname(), 'rb');
stream_copy_to_stream($file, $out, $this->maxlen, $this->offset);
fclose($out);
fclose($file);
if ($this->deleteFileAfterSend && file_exists($this->file->getPathname())) {
unlink($this->file->getPathname());
}
if ($this->deleteDirAfterSend && file_exists($this->deleteDir)) {
rmdir($this->deleteDir);
}
return $this;
}
/**
* If this is set to true, the file will be unlinked after the request is send
* Note: If the X-Sendfile header is used, the deleteDirAfterSend setting will not be used.
*
* @param bool $shouldDelete
*
* @param null $dir
* @return $this
*/
public function deleteDirAfterSend($shouldDelete = true, $dir = null)
{
$this->deleteDirAfterSend = $shouldDelete;
$this->deleteDir = $dir;
return $this;
}