在php中将文件从Linux下载到Windows时修复文件编码

好我有问题。 我有一个Linux Web服务器(带有apache 2的RHEL 4),用于存放应用程序。 这个应用程序的一部分是一组PHP脚本。 我创建了一个接受一些表单变量的脚本,然后将文件下载到用户。 这里是代码:</ p>

  header('Content-Description:File Transfer'); 
header('Content-Type:application / octet-stream');
header( 'Content-Disposition:attachment; filename ='。$ destFileName);
header('Expires:0');
header('Cache-Control:must-revalidate,post-check = 0,pre-check = 0') ;
header('Pragma:public');
header('Content-Length:'。filesize($ fullPath));
ob_clean();
flush();
readfile($ fullPath);
</ code > </ pre>

这一切正常,文件下载。 但有一个问题。 这些文件正从Linux机器下载到Windows机器(ALWAYS)。 问题是编码。 当您查看Linux机器上的文件时,所有文本都是对齐的,并且所有列都很好。 (这些文件只是平面文本文件)。 但是当文件被下载到Windows框中并在记事本中打开时,该文件全部被搞砸,并且没有任何对齐。 你也看到了奇怪的特征(看起来像一个盒子,但这只是unknow字符的通用表示)。 当此文件导入到另一个程序时,它不起作用。</ p>

但是,当我在写字板中打开文件时,所有文本看起来都是正确的。 如果我从wordpad保存它将正确导入,并在Notpad中看起来正确。</ p>

我对文件编码知之甚少,所以有关如何编码文件的任何信息 发送给用户下载会很棒。</ p>

我尝试用以下代码替换 readfile($ fullPath); </ code>:</ p>

< pre> $ handle = @fopen($ fullPath,“r”);
if($ handle){
while(!feof($ handle)){
$ buffer = fgets($ handle); \ n $ buffer = str_replace('
','
',$ buffer);
echo $ buffer;
}
fclose($ handle);
}
</ code> </ pre>

谢谢!</ p>
</ div>

展开原文

原文

Ok I have an issue. I have a Linux web server (RHEL 4 with apache 2) that is used to house an application. Part of this application is a set of php scripts. I created a script that accepts some form variables and then downloads a file to the user. Here si the code:

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$destFileName);
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($fullPath));
ob_clean();
flush();
readfile($fullPath);

This all works fine, and the file gets downloaded. But there is a problem. These files are being downloaded from the Linux box, to a Windows machine (ALWAYS). The problem is the encoding. When you look at the file on the Linux box all the text is aligned and all the columns look fine. (The files are just flat text files). But when the file gets downloaded onto the Windows box, and opened in Notepad, the file is all fouled up, and nothing is aligned. You also see weird charactors (the ones that look like a box, but that is just the generic representation for the unknow charactor). When this file is imported into another program, it does not work.

However, when I open the file up in WordPad, all the text look correct. If I save it from wordpad it will import correctly, and look correct in Notpad.

I don't have much knowlege on file encoding, so any information on how I can encode the file before sending to the user for download would be great.

I did try replacing the readfile($fullPath); with:

$handle = @fopen($fullPath, "r");
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle);
        $buffer = str_replace('
', '
', $buffer);
        echo $buffer;
    }
    fclose($handle);
}

Thanks!

douqiaoru2583
douqiaoru2583 以防任何人来到这里遇到类似的问题:如果换行符在记事本/编辑器中变成了框但在写字板中看起来很好,那就是不使用Windows换行符(由于某种原因,记事本对于换行符非常肛门,而写字板是正确的)。如果它是其他角色,你可能正在处理编码问题(例如UTF-8被视为Windows-1252或US-ASCII)。
大约 11 年之前 回复

3个回答



以下行存在问题:</ p>

  $ buffer = str_replace('

','
',$ buffer);
</ code> </ pre>

您需要在那里使用双引号。 “
”是换行符。 '
'是文字字符串序列反斜杠-n:</ p>

  #php -r“var_dump('
',\”
\“);”
string (2)“

string(1)“

</ code> </ pre>
</ div>

展开原文

原文

There's an issue with the following line:

$buffer = str_replace('
', '
', $buffer);

You'd need to use double quotes there. " " is newline. ' ' is the literal char sequence backslash-n:

# php -r "var_dump('
', \"
\");"
string(2) "
"
string(1) "
"



有一个Unix实用程序'unix2dos'和'dos2unix'可能会有所帮助。 您可以通过php将其作为系统调用来调用。</ p>

或者,我确定有相同的php版本。</ p>

但是 我不是个骗子。</ p>
</ div>

展开原文

原文

There is a Unix utility 'unix2dos' and 'dos2unix' that might help. You could call it from php as a system call.

Or, I'm sure there is a php version of the same thing.

But I'm not a php guy.

dongwuzun4630
dongwuzun4630 啊,你必须让它为你修改文件。 sys('unix2dos',$ fullPath,$ fullPath。“_ dos”)然后发送_dos文件。 然后删除它。 不理想。
大约 11 年之前 回复
doutang1946
doutang1946 我喜欢这个想法,因为它可能不仅仅是将新行更新为DOS格式。 问题是,当我尝试从php执行它时,我没有得到任何回应。 任何人都有任何帮助。
大约 11 年之前 回复

EDIT: I did not know that about PHP quoting. Still, you may need to decide on a standard encoding when multiple languages will be used, the rest of this post is still valid.

Windows generally uses ANSI or ASCII encoded files for text, using the character set that is local to the system. (Like cp1252).

It might be easiest just to encode it all in UTF8, and then tell notepad to read the file in as a UTF8 document. (It is a drop-down in the File->Open dialog.)

I do not see a way to specify the encoding from the command-line, and I am not sure that notepad will find it automatically.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问