duanmei1930 2014-06-05 07:09
浏览 31
已采纳

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

I have two php functions that are called upon consecutive requestes:

This is called on the first request:

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);
}

The following function is called on the second request:

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;
}

The $response_content is empty. If I comment the unlink and unset, I get the appropriate file contents. Why is that ?

I want to download the returned value of 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();

}
  • 写回答

1条回答 默认 最新

  • doutian4046 2014-06-05 08:14
    关注

    Before you can use ob_end_clean(), you have to use ob_start(). Otherwise, there's no output buffer to clear out.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据