php下载文件:header() [英] php download file: header()

查看:157
本文介绍了php下载文件:header()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些教育。
在每个月的月底,我想从我的网络服务器上下载一些数据到我的本地PC。
所以,我已经为此写了一个脚本,它从DB中选择数据。
接下来,我想下载它。
我试过这个:

  $ file = $ month。 '。文本'; 
$ handle = fopen($ file,w);
header(Content-Type:application / text);
header(Content-Disposition:attachment,filename =。$ month。'.txt');
while($ row = mysql_fetch_array($ res))
{
$ writestring = $ row ['data_I_want']。 \r\\\
;
fwrite($ handle,$ writestring);
}
fclose($ handle);

如果我运行这个,那么该文件会被创建,但是我的文件不包含数据我想要。相反,我从我的浏览器中的HTML文件中获得了一个转储。
我在做什么错误..
谢谢,
Xpoes

  //下面是在哪里你创建特定月份的文本文件
$ file = $ month。 '。文本';
$ handle = fopen($ file,w);
while($ row = mysql_fetch_array($ res)){
$ writestring = $ row ['data_I_want']。 \r\\\
;
fwrite($ handle,$ writestring);
}
fclose($ handle);
//现在文件准备好来自数据库

的数据//下面添加下载文本文件
$ filename = $ file; //文件名
$ filepath = $ file; //文件的位置。因为你的文件是在这个脚本是
header(Cache-control:private)的同一个文件夹上创建的,所以我放了$ file。
header(Content-type:application / force-download);
header(Content-transfer-encoding:binary \\\
);
header(Content-disposition:attachment; filename = \$ filename \);
header(Content-Length:.filesize($ filepath));
readfile($ filepath);
出口;


I need some eduction please. At the end of each month, I want to download some data from my webserver to my local PC. So, I've written a little script for that, which selects the data from the DB. Next, I want to download it. I've tried this:

$file=$month . '.txt';
$handle=fopen($file, "w");
header("Content-Type: application/text");
header("Content-Disposition: attachment, filename=" . $month . '.txt');
while ($row=mysql_fetch_array($res))
    {
    $writestring = $row['data_I_want'] . "\r\n";
    fwrite($handle, $writestring);
    }
fclose($handle);

If I run this, then the file is created, but my file doesn't contain the data that I want. Instead I get a dump from the HTML-file in my browser.. What am I doing wrong.. Thanks, Xpoes

解决方案

Below script will help you download the file created

//Below is where you create particular month's text file
$file=$month . '.txt';
$handle=fopen($file, "w");
while ($row=mysql_fetch_array($res)){
    $writestring = $row['data_I_want'] . "\r\n";
    fwrite($handle, $writestring);
}
fclose($handle);
//Now the file is ready with data from database

//Add below to download the text file created
$filename = $file; //name of the file
$filepath = $file; //location of the file. I have put $file since your file is create on the same folder where this script is
header("Cache-control: private");
header("Content-type: application/force-download");
header("Content-transfer-encoding: binary\n");
header("Content-disposition: attachment; filename=\"$filename\"");
header("Content-Length: ".filesize($filepath));
readfile($filepath);
exit;

这篇关于php下载文件:header()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆