dsb238100 2014-08-21 00:27
浏览 9
已采纳

提供可变内容的文件下载。 PHP

If we have the following

<form method="post" action="">
<input type="hidden" name="token" value="1">
<input type="submit">
</form>

<?php

if(isset($_POST['token']))
{    

    $v = 'NAME,COLOR
          jack,blue
          ivan,gold';

    header("Content-Type: text/csv");
    header("Content-Disposition: attachment; filename=\"file.csv\"");
    header('Expires: ' . gmdate('D, d M Y H:i:s', gmmktime() - 3600) . ' GMT');
    //header("Content-Length: " . filesize($path));

    echo $v;

}

When I run the script, the browser prompts me to download, when the file is downloaded and I check the contents, it also contains the HTML(above the $v data) markup in the file.

I wish to have a file consisting of just the $v variable data.

  • 写回答

1条回答 默认 最新

  • dongnao3990 2014-08-21 00:44
    关注

    Anything you output will be output. An HTTP request is for one resource at a time. You can't expect to send a web page and not expect to have that as part of your file content. PHP cannot read your mind and know what part of your code is for the browser, and what part is for the downloaded resource.

    You need to make a separate script that does nothing but return a single resource... the file to be downloaded. Start your script with <?php, no whitespace. If your file is purely PHP (and it probably will be), don't end it with ?> to avoid whitespace at the end.

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

报告相同问题?

悬赏问题

  • ¥15 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类