Php 来自zip的文件响应

Php 来自zip的文件响应,php,file,zip,response,Php,File,Zip,Response,以下是我正在尝试做的:我已经使用包含多个文件的类创建了zip存档,我现在需要做的是打开存档,读取单个文件,然后在浏览器中下载或打开它 由于我使用的是symfony2框架,如果它是一个常规文件,我可以这样做: case 'open': $response = new BinaryFileResponse($filepath); $response->headers->set('Content-type', mime_content_type($filepath));

以下是我正在尝试做的:我已经使用包含多个文件的类创建了zip存档,我现在需要做的是打开存档,读取单个文件,然后在浏览器中下载或打开它

由于我使用的是symfony2框架,如果它是一个常规文件,我可以这样做:

case 'open':
    $response = new BinaryFileResponse($filepath);
    $response->headers->set('Content-type', mime_content_type($filepath));
    $response->setContentDisposition(
        ResponseHeaderBag::DISPOSITION_INLINE,
        $filename
    );
    return $response;
case 'save':
    $response = new BinaryFileResponse($filepath);
    $response->headers->set('Content-type', mime_content_type($filepath));
    $response->setContentDisposition(
        ResponseHeaderBag::DISPOSITION_ATTACHMENT,
        $filename
    );
    return $response;
但由于文件不在任何目录中,我可以将其传递给BinaryFileResponse类,因为它只接受文件或SplFileInfo对象的字符串路径,而不接受文件内容

我发现以下内容让我想到了从文件内容创建SplFileObject,然后将其作为SplFileInfo对象传递给BinaryFileResponse类,因为SplFileObject扩展了SplFileInfo,所以我做了以下工作:

$tmp = 'php://temp';
$file = new \SplFileObject($tmp, 'w+');
$file->fwrite($filecontents);
然后将$file传递给BinaryFileResponse类,但它抛出错误:文件“php://temp“不存在。我不知道我是否在做这样的事情,如果是这样,我错过了什么

在任何情况下,我不想实现的是以两种不同的方式为归档文件提供服务:1。downlaod,2.在浏览器中打开

这些文件是PDF格式的。如果我创建响应对象并将其内容设置为存档文件的内容,我就可以打开它们,但这样就无法直接下载


很抱歉,如果让人困惑,请提前感谢您的帮助。

我最终想到的是:

//1. Extract file to chosen directory
$zip = new \ZipArchive();
if ($zip->open('file/path/file.zip') {
    $zip->extractTo('chosen/directory', array('filename_in_zip_archive.ext'));
    $zip->close();
}

//2. Put file in response
$response = new Response(file_get_contents('chosen/directory/filename_in_zip_archive.ext'));
$mime = new \finfo(FILEINFO_MIME_TYPE);
$response->headers->set('Content-type', $mime->file('chosen/directory/filename_in_zip_archive.ext'));

//3. logic to open or download file
case 'open':
    $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_INLINE, 'filename_in_zip_archive.ext');
case 'save': 
    $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'filename_in_zip_archive.ext');


//4. After file has been put to response, delete local file copy
if (file_exists('chosen/directory/filename_in_zip_archive.ext')) {
    unlink('chosen/directory/filename_in_zip_archive.ext');
}

//5. Return response with file
return $response;