什么时候做 fopen PHP 会自己复制文件?



>任何人都可以向我解释这段代码是如何工作的。

我的疑问是,如果我删除了该文件,我怎么仍然可以使用已删除的文件进行正确的下载。

PHP,当我们做一个fopen,将文件的副本保存在任何地方?

$response = Yii::$app->response->sendFile($file, 'download-file.zip');
// Before I send a response I delete the file.
unlink($file);
// Works without issues. The file is downloaded.
return $response;

只是关于发送文件功能的说明。

// Yii in sendFile does a fopen and sends the file as stream
$handle = fopen($filePath, 'rb');
$this->sendStreamAsFile($handle, $attachmentName, $options);

谢谢。

这是由于 linux 处理文件删除的方式。在最后一个句柄关闭之前,数据仍可访问。

https://linux.die.net/man/3/remove

如果名称是文件的最后一个链接,但任何进程仍打开该文件,则该文件将一直存在,直到引用它的最后一个文件描述符关闭。

自 PHP 7.3.0 以来,此方法也适用于 Windows,但文件名一直使用,直到最后一个句柄关闭。

最新更新