我想从php下载生成的文件。它工作但它在文件的开头添加了2个空行。我尝试通过winscp下载文件并比较它们,这个通过php下载的文件在标题中有2个空行。我错了2天这个错误,我无法解决这个问题。请你帮助我好吗 ? :)
由于
<?php
include 'connect.php';
function trsgen($mid=null)
{
if(isset($mid) && $mid!=null)
{
include 'connect_oo.php';
$query="My query;";
$result=$conn->query($query);
$File=$mid . '.trs';
$myfile = fopen($File, "w");
$i=1;
if($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
if(isset($i) && $i==1)
{
$nameqry="another query;";
$nameresult=$conn->query($nameqry);
if($nameresult->num_rows == 1)
{
$name = $nameresult->fetch_assoc();
$medianame=$name["media_name"];
}
else
{
$medianame=null;
}
$timeqry="SELECT distinct etime FROM subtitles where media_id=$mid order by stime DESC LIMIT 1;";
$timeresult=$conn->query($timeqry);
if($timeresult->num_rows == 1)
{
$time = $timeresult->fetch_assoc();
$endtime=$time["etime"];
}
else
{
$endtime='1.0';
}
$hla1='<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL;
$hla2='<!DOCTYPE Trans SYSTEM "trans-14.dtd">'.PHP_EOL;
$hla3='<Trans scribe="tommy" audio_filename="'.$medianame.'" version="1" version_date="150302">'.PHP_EOL;
$hla4='<Episode>'.PHP_EOL;
$hla5='<Section type="report" startTime="0" endTime="'.$endtime.'">'.PHP_EOL;
$hla6='<Turn startTime="0" endTime="'.$endtime.'">'.PHP_EOL;
fwrite($myfile, $hla1);
fwrite($myfile, $hla2);
fwrite($myfile, $hla3);
fwrite($myfile, $hla4);
fwrite($myfile, $hla5);
fwrite($myfile, $hla6);
}
$stime='<Sync time="'.$row["stime"].'"/>'.PHP_EOL;
$text=$row["text"].PHP_EOL;
$etime='<Sync time="'.$row["etime"].'"/>'.PHP_EOL;
fwrite($myfile, $stime);
fwrite($myfile, $text);
fwrite($myfile, $etime);
if($result->num_rows == $i)
{
$tail1='</Turn>'.PHP_EOL;
$tail2='</Section>'.PHP_EOL;
$tail3='</Episode>'.PHP_EOL;
$tail4='</Trans> '.PHP_EOL;
fwrite($myfile, $tail1);
fwrite($myfile, $tail2);
fwrite($myfile, $tail3);
fwrite($myfile, $tail4);
}
$i++;
}
fclose($myfile);
$conn->close();
$file = __DIR__ . '/'.$mid.'.trs' ;
header('Content-disposition: attachment; filename='.$File.'');
header('Content-type: application/trs');
header("Content-length: ".filesize($file));
readfile($file);
unlink($File);
}
}
}
?>
<?php
if(isset($_POST["mid"]) && !empty($_POST["mid"]))
{trsgen($_POST["mid"]);}
?>
<form action="" method="POST">
<select name="mid">
<?php
$query1="SELECT * FROM media where media_disabled!=1 ORDER BY mid DESC;";
$result1=$conn->query($query1);
if($result1->num_rows > 0)
{
while($row1 = $result1->fetch_assoc())
{
echo '<option value="'.$row1["mid"].'">'.$row1["mid"].'-'.$row1["media_name"].'</option>';
}
}
$conn->close();
?>
<input type="submit" value="Generuj trs subor"></input>
</select>
</form>
答案 0 :(得分:0)
在PHP中,您可以尝试使用正则表达式替换空行。我会确保你的程序每次/每次下载文件时都有2个空行。
答案 1 :(得分:0)
我自己想出来了。
我创建了单独的文件,仅用于下载文件download.php。
在主要php:
...
...
fclose($myfile);
$conn->close();
header("Location: http://sub.radio9.sk/tmp/trsgen/download?mid=$mid");
}
并在download.php中
<?php
if(isset($_GET["mid"]) && !empty($_GET["mid"]))
{
$mid=$_GET["mid"];
$file=$mid.'.trs';
header('Content-disposition: attachment; filename='.$file.'');
header('Content-type: application/download');
header("Content-length: ".filesize($file));
readfile($file);
unlink($file);
}
?>
然后两条空行消失了。 :)