什么时候执行 fopen PHP 将文件复制到自身?

When doing fopen PHP makes a copy of the file to itself?

任何人都可以向我解释这段代码的工作原理。

我的疑问是,如果我删除了文件,我怎样才能使用已删除的文件正确下载。

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;

请注意 sendFile 的作用。

// 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

If the name was the last link to a file, but any processes still have the file open, the file will remain in existence until the last file descriptor referring to it is closed.

从 PHP 7.3.0 开始,此方法也适用于 Windows,但文件名在最后一个句柄关闭之前一直在使用。