PHP 强制下载在文档开头添加 2 行
PHP forced download add 2 lines in the begining of document
我想从 php 下载生成的文件。它有效,但它在文件开头添加了 2 个空行。我尝试通过 winscp 下载文件并比较它们,这个通过 php 下载的文件在 header 中有 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>
在PHP中,您可以尝试用正则表达式替换空白行。我会确保 each/every 你的程序下载文件时有 2 个空行。
How do I remove blank lines from text in PHP?
我自己想出来的
我已经为下载文件 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);
}
?>
然后两个空行就消失了。 :)
我想从 php 下载生成的文件。它有效,但它在文件开头添加了 2 个空行。我尝试通过 winscp 下载文件并比较它们,这个通过 php 下载的文件在 header 中有 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>
在PHP中,您可以尝试用正则表达式替换空白行。我会确保 each/every 你的程序下载文件时有 2 个空行。
How do I remove blank lines from text in PHP?
我自己想出来的
我已经为下载文件 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);
}
?>
然后两个空行就消失了。 :)