dsvtnz6350 2011-11-24 09:49
浏览 54
已采纳

在PHP中创建临时.htm文件并将其发送给用户?

Is there a simple way to create an empty temp .htm file on the server and send it to the user as download? I don't need to really create the file with fopen(), it's enough to just create it temporarily and send it to the user.

I need that for a website authentication. I found there is tmpfile() function within PHP, but it doesn't quite work for me yet. I am working on a Symfony project, maybe there are also a header function I don't know of.

All I need is:

$token = '12345';
$filename = $token . '.htm';
createatmpfile($filename);
header(send .htm file as download to the user);

Not sure how that works, but maybe my pseudocode above explains what I am looking for ..

EDITED:

That's how it works for Symfony2 (thanks Grad):

use Symfony\Component\HttpFoundation\Response;

Controller method:

public function generateAction() {
    $response = new Response();
    $response->headers->set('Content-Disposition', 'attachment; filename="'.$token.'.htm"'); 
    return $response;
}
  • 写回答

1条回答 默认 最新

  • dongzhuo2371 2011-11-24 10:12
    关注

    In plain PHP you could use to following line to force the user to download the response

    header('Content-Disposition: attachment; filename="filename.htm"');
    

    In Symfony you can use:

    $this->getResponse()->setHttpHeader('Content-Disposition', 'attachment; filename="filename.htm"');
    

    With this header set you can either just execute your controller (and let the view fill in the response), or use $contents = file_get_contents($path) together with return $this->renderText($contents).

    But depending on what you're trying to achieve: you don't have to create a tempfile to force the to download it.

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

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100