douju7503 2014-02-06 10:46 采纳率: 0%
浏览 40

PHP mysql创建一个文本文件供下载以及数据显示

I have a php scipt which accepts a date as input and get the data from mysql table and display a summary of various prameters pertaining to retrieved data on the page. It also generate a text file containing formatted data and provide a donload link for the same.

My concern is, every time when this page is called, a text file is created for that date and stored in the server. I have to manually delete it. Instead I want to create the textfile on the fly and get it deleted after the user download it. the script is below.

<?php
$db=mysql_select_db($database_met,$met) or die("could not connect");

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



$newdate = strtotime ( '-1 day' , strtotime ( $_POST['stddate'] ) ) ;
    $fromdate= date('Y-m-d', $newdate)." 01:00:00";
    $todate=$_POST['stddate']." 00:00:00";

    //echo $fromdate."</br>".$todate;
//$datetime=$_POST['stddate']."00:00";

$result=mysql_query(" SELECT Max(temp_max_60) as maxtemp60, Min(temp_min_60) as mintemp60, Max(temp_max_10) as maxtemp10, Min(temp_min_10) as mintemp10, Max(rh_max) as maxrh, Min(rh_min) as minrh,  Max(speed_max_60) as maxspeed60,  sum(case when speed_avg_60 < 0.83 then 1 else 0 end) as calm, count(*) as readings from tower_log where date between '$fromdate' and '$todate'");   

$pdr=mysql_query(" SELECT dir_avg_60 from tower_log where date between '$fromdate' and '$todate'");



}

else{
$newdate = strtotime ( '-1 day' , strtotime ( $altto ) ) ;
    $fromdate= date('Y-m-d', $newdate)." 01:00:00";
    $todate=$altto." 00:00:00";




$result=mysql_query(" SELECT Max(temp_max_60) as maxtemp60, Min(temp_min_60) as mintemp60, Max(temp_max_10) as maxtemp10, Min(temp_min_10) as mintemp10, Max(rh_max) as maxrh, Min(rh_min) as minrh,  Max(speed_max_60) as maxspeed60,  sum(case when speed_avg_60 < 0.83 then 1 else 0 end) as calm, count(*) as readings from tower_log where date between '$fromdate' and '$todate'");   

$pdr=mysql_query(" SELECT dir_avg_60 from tower_log where date between '$fromdate' and '$todate'");

}
$fd=array();


$fd = array(
array(
'sector' => 'N',
'freq' => 0
),
array(
'sector' => 'NNE',
'freq' => 0),
array(
'sector' => 'NE',
'freq' => 0),
array(
'sector' => 'ENE',
'freq' => 0),
array(
'sector' => 'E',
'freq' => 0),
array(
'sector' => 'ESE',
'freq' => 0),
array(
'sector' => 'SE',
'freq' => 0),
array(
'sector' => 'SSE',
'freq' => 0),
array(
'sector' => 'S',
'freq' => 0),
array(
'sector' => 'SSW',
'freq' => 0),
array(
'sector' => 'SW',
'freq' => 0),
array(
'sector' => 'WSW',
'freq' => 0),
array(
'sector' => 'W',
'freq' => 0),
array(
'sector' => 'WNW',
'freq' => 0),
array(
'sector' => 'NW',
'freq' => 0),
array(
'sector' => 'NNW',
'freq' => 0),
);

$a=$b=$c=$d=$e=$f=$g=$h=$i=$j=$k=$l=$m=$n=$o=$p=0;
while($result_pdr=mysql_fetch_array($pdr)){

$dir=$result_pdr['dir_avg_60'];


switch ($dir){

case  ($dir<11.25 || $dir>348.75):
$fd[0]['freq']++;

break;

case  ($dir>=11.25 && $dir<33.75):
$fd[1]['freq']++;
break;

case  ($dir>=33.75 && $dir<56.25):
$fd[2]['freq']++;
break;

case  ($dir>=56.25 && $dir<78.75):
$fd[3]['freq']++;
break;

case  ($dir>=78.75 && $dir<101.25):
$fd[4]['freq']++;
break;

case  ($dir>=101.25 && $dir<123.75):
$fd[5]['freq']++;
break;

case  ($dir>=123.75 && $dir<146.25):
$fd[6]['freq']++;
break;

case  ($dir>=146.25 && $dir<168.75):
$fd[7]['freq']++;
break;

case  ($dir>=168.75 && $dir<191.25):
$fd[8]['freq']++;
break;

case  ($dir>=191.25 && $dir<213.75):
$fd[9]['freq']++;
break;

case  ($dir>=213.75 && $dir<236.25):
$fd[10]['freq']++;
break;

case  ($dir>=236.25 && $dir<258.75):
$fd[11]['freq']++;
break;

case  ($dir>=258.75 && $dir<281.25):
$fd[12]['freq']++;
break;

case  ($dir>=281.25 && $dir<303.75):
$fd[13]['freq']++;
break;

case  ($dir>=303.75 && $dir<326.25):
$fd[14]['freq']++;
break;

case  ($dir>=326.25 && $dir<348.75):
$fd[15]['freq']++;
break;
}


}


function get_highest($arr)
{
   foreach($arr as $key => $val)
   {
      if ( is_array($val) ) $arr[$key] = get_highest( $val );
   }

   sort($arr);

   return array_pop($arr);
}



$num_records=mysql_num_rows($result);

//echo $newdate;

$record=mysql_fetch_array($result);
echo "<div></br></br></br>";
if(isset($_POST['from'])){
echo "Date:".date('d-m-Y', $newdate)."</br>";}
else{"Date:".date('d.m.Y',strtotime("-1 days"))."</br>";}
echo "No. of readings".$record['readings']."</br></br></br>";
echo "</div>";
echo "<table class='altrowstable'>";
echo "<tr><th rowspan='2'>Max. Wind Speed m.s<sup>-1</sup></th><th rowspan='2'>Calm(%)</th><th colspan='2'>Temp. 10m</th><th colspan='2'>Temp. 60m</th><th colspan='2'>Humidity</th></tr><tr><th>Min</th><th>Max</th><th>Min</th><th>Max</th><th>Min</th><th>Max</th</tr>";
echo "<tr><td>".$record['maxspeed60']."</td><td>".number_format($record['calm']*100/$record['readings'],1)."</td>";
//echo "</table>";
//echo "<br/><table class='altrowstable'>";
//echo "<tr><th></th><th>Max. </sup></th><th>Min</th></tr>";
echo "<td>".$record['maxtemp10']."</td><td>".$record['mintemp10']."</td>";
echo "<td>".$record['maxtemp60']."</td><td>".$record['mintemp60']."</td>";
echo "<td>".$record['maxrh']."</td><td>".$record['minrh']."</td></tr>";
echo "</table></br>";

$highval= get_highest($fd);


ksort($fd);



echo "<table class='altrowstable'>";

echo "<tr><td>Sector</td>";
foreach ($fd as $key => $value) {

if($fd[$key]['freq']==$highval){echo "<td class='pdr'>";} else { echo "<td>";}
echo $fd[$key]['sector']."</td>";
}
echo "</tr><tr><td>No.of Occurances</td>";

foreach ($fd as $key => $value) {

if($fd[$key]['freq']==$highval){echo "<td class='pdr'>";} else { echo "<td>";}
echo $fd[$key]['freq']."</td>";
}
echo "</tr></table>";


$knt=mysql_query("SELECT *from tower_log where date between '$fromdate' and '$todate' ");

$new_knt="reports/".substr($fromdate,0,4).substr($fromdate,5,2).substr($fromdate,8,2).".knt";
$knt_handle=fopen($new_knt,"w");
while ($knt_rec=mysql_fetch_array($knt)){


$datestring= "DD".substr($knt_rec['date'],8,2).substr($knt_rec['date'],5,2).substr($knt_rec['date'],0,4).substr($knt_rec['date'],11,2)."00";

$line=$datestring." ". sprintf('%03d',$knt_rec[1])." ".sprintf('%03d',$knt_rec[2])." ".sprintf('%03d',$knt_rec[3])." ".sprintf('%03d',$knt_rec[4])." ".sprintf('%03d',$knt_rec[5])." ".sprintf('%03d',$knt_rec[6])." ".sprintf('%03d',$knt_rec[7])." ".sprintf('%03d',$knt_rec[8])." "." ".sprintf('%04.1f',$knt_rec[9])." ".sprintf('%04.1f',$knt_rec[10])." ".sprintf('%04.1f',$knt_rec[11])." ".sprintf('%04.1f',$knt_rec[12])." ".sprintf('%04.1f',$knt_rec[13])." ".sprintf('%04.1f',$knt_rec[14])." ".sprintf('%04.1f',$knt_rec[15])." ".sprintf('%04.1f',$knt_rec[16])." ".sprintf('%04.1f',$knt_rec[17])." ".sprintf('%04.1f',$knt_rec[18])." ".sprintf('%04.1f',$knt_rec[19])." ".sprintf('%04.1f',$knt_rec[20])." ".sprintf('%04.1f',$knt_rec[21])." ".sprintf('%04.1f',$knt_rec[22])." ".sprintf('%04.1f',$knt_rec[23])." ".sprintf('%04.1f',$knt_rec[24])." ".sprintf('%04.1f',$knt_rec[25])." ".sprintf('%04.1f',$knt_rec[26])." ".sprintf('%04.1f',$knt_rec[27])." ".sprintf('%04.1f',$knt_rec[28])." ".sprintf('%04.1f',$knt_rec[29])." ".sprintf('%04.1f',$knt_rec[30])." ".sprintf('%04.1f',$knt_rec[31])." ".sprintf('%04.1f',$knt_rec[32])." ".sprintf('%04.1f',$knt_rec[33])." ".sprintf('%04.1f',$knt_rec[34])." ".sprintf('%04.1f',$knt_rec[35])." ".sprintf('%04.1f',$knt_rec[36])." ".sprintf('%04.1f',$knt_rec[37])." ".sprintf('%04.1f',$knt_rec[38])." ".sprintf('%04.1f',$knt_rec[39])." ".sprintf('%04.1f',$knt_rec[40])." ".sprintf('%04.1f',$knt_rec[41])." ".sprintf('%04.1f',$knt_rec[42])." ".sprintf('%04.1f',$knt_rec[43])." ".sprintf('%04.1f',$knt_rec[44])." ".sprintf('%04.1f',$knt_rec[45]);


$new_line=$line."
";
fwrite($knt_handle,$new_line);


}


echo '<br/><br/><a href="/Dept/esl/'.$new_knt.'">Download KNT File</a>';

?>

I doubt whether there is some ambiguity in my question. The text file is already created with the fopen line. I want to delete the file or need another way to put the data for download

  • 写回答

2条回答 默认 最新

  • doulaopu2343 2014-02-06 10:51
    关注

    Maybe use unlink('Path_of_your_text_file') ?

    PHP DOC Unlink()

    When your user click on Download, store the path of your file somewhere, and if it's just a simple text, delete your file after 5 minutes ? with CRON or don't know...

    Edit :

    Try this code (and adapt to yours):

    header('Content-type:  application/zip');
    header('Content-Length: ' . filesize($file));
    header("Content-Disposition: attachment; filename="file.zip");
    readfile($file);
    
    ignore_user_abort(true);
    if (connection_aborted()) {
    unlink('YOUR_FILE');
    }
    

    connection_aborted()

    ignore_user_abort()

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么