简体   繁体   English

从服务器PHP下载文件-文件略有关闭

[英]Downloading file from server PHP - File is slightly off

I hope this is a simple and quick fix. 我希望这是一个简单快捷的解决方法。 I have looked on here already to learn how to force download a file from the server. 我已经在这里查看了有关如何强制从服务器下载文件的信息。 Here is what I am using below: 这是我在下面使用的:

<?php
// Sending the file - a pdf in this case
header('Content-type: application/octet-stream');

// Specify what the  file will be called
header('Content-Disposition: attachment; filename="1234.txt"');

// And specify where it is coming from 
readfile('C:\test\1234.txt');
?>

It should be pretty self explanatory but I have a text file saved at C:\\test\\1234.txt. 这应该是很自我解释的,但是我有一个文本文件保存在C:\\ test \\ 1234.txt。

I link to my php file (that has the above code) which is called download.php using this from the HTML page: 我链接到我的php文件(具有上面的代码),该文件从HTML页面使用以下文件称为download.php:

<a href="/download.php" title="Download This PDF">Download This File!</a>

OK now the problem.. My original text file is this: 现在好了。..我的原始文本文件是这样的:

test

but when I download the file, the result is a carriage return above: 但是当我下载文件时,结果是上面的回车:

_
test

The problem isn't huge with the text file, but because of this issue, all other files downloaded are corrupt and I believe this is why. 文本文件的问题并不大,但是由于这个问题,所有其他下载的文件都损坏了,我相信这就是原因。 I am hoping that someone has a solution to this that is pretty simple. 我希望有人对此有一个简单的解决方案。

Thanks in advance! 提前致谢!

You might try removing the PHP closing tag. 您可以尝试删除PHP结束标记。 Note this quote from php.net 注意来自php.net的引用

If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. 如果文件是纯PHP代码,则最好在文件末尾省略PHP结束标记。 This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script. 这样可以防止在PHP结束标记之后添加意外的空格或换行,这可能会导致不良影响,因为当程序员不打算在脚本中的该点发送任何输出时,PHP将开始输出缓冲。

Perhaps that's where you're getting the extra carriage return. 也许那是您获得额外回车的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM