将删除后文件的内容作为新文件下载返回



我有两个php函数在连续请求时被调用:

在第一个请求时调用:

public static function ProcessResponseFile($request_file_content)
{
    session_start();
    $temp_file = tempnam("./tmp", "file"); 
    file_put_contents($temp_file, $request_file_content);
    $_SESSION['request_response'] = $temp_file;
    return file_get_contents($temp_file);
}

在第二个请求时调用以下函数:

public static function GetResponseFileContent()
{
    session_start(); 
    $filename = $_SESSION['request_response'];
    $handle = fopen($filename, "r");
    $response_content = fread($handle, filesize($filename));
    fclose($handle);
   // $response_content = file_get_contents($_SESSION['request_response']);
    unlink($_SESSION['request_response']);
    unset($_SESSION['request_response']);
    return $response_content;
}

$response_content为空。如果对unlink和unset进行注释,就会得到适当的文件内容。为什么?

我想下载GetResponseFileContent()的返回值

if (isset($_POST['submit_download'])) {
    ob_end_clean();
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=raspuns.xml");
    header("Content-Type: application/octet-stream; "); 
    header("Content-Transfer-Encoding: binary");
    session_start();
    echo Application::GetResponseFileContent();
}

在你可以使用ob_end_clean()之前,你必须使用ob_start()。否则,没有输出缓冲区可以清除。

最新更新