English 中文(简体)
Fwrite - 这被执行吗?
原标题:Fwrite - is this executed?
  • 时间:2010-02-11 12:57:13
  •  标签:
  • php
  • fwrite

I have a bunch of fwrites that write to a text file. However, its seem that the new line (" ") that I want after each row never gets put in since the next set of data is stuck to the last line that this code inserts into the text file:

while(odbc_fetch_row($result)){

        if($counter==0){

            $counter3 = 1;

            foreach($column_names as $column_names2){

                if($length!=$counter3){

                    fwrite($write_file,  " .$column_names2. ", );

                }else{

                    fwrite($write_file,  " .$column_names2. " );

                }

                $counter3++;

            }

            //echo  Never gets executed??? ;
            fwrite($write_file, "
");

            $counter = 1;

        }

有任何想法吗?

I have put in " " as a quick test. What is strange is if I view in this in notepad it is still stuck together, but if I view it in wordpad it shows a line break?!

最佳回答

正如其他人所建议的那样,这很可能是由于新行在您的操作系统上的编码方式引起的。

However, rather than explicitly output a Windows-specific " ", I suggest that you try opening the file in text (rather than binary) mode. The advantage of opening in text mode is that the system will convert the line endings to whatever format is appropriate for that OS. That way, your code should be more portable, and should (hopefully) work properly on Windows/Linux/Mac etc.

假设你正在使用PHP,可以将“文本模式翻译标志”(t)传递到fopen。

问题回答

代码看起来没问题。

Can it be that you are lookign at the file with a Windows application that can t handle line breaks? What happens if you substitute the by something else?

It s not a problem with your code, but a problem with your viewer. Changing viewers will solve your problem. Changing line terminators to something your viewer understands ( ) will also solve the problem. The important thing is to write the line terminator in a way that the program that will later be consuming it will understand. If it s just the viewer, then change the line terminator. If its another program that expects just a newline character, change viewers.

阅读关于各种操作系统中的换行符的内容

Windows indicates a new line by using CR + LF (i.e. ) while UNIX(-like) systems only use LF ( ).

CR = Carriage return
LF = Line feed

So you probably have to use . But other programs like Wordpad seem to understand -only too.


最好的方法是使用 PHP 常量 PHP_EOL,它使用当前操作系统的换行符指示符,脚本正在运行:

fwrite($write_file, PHP_EOL);

you can try using

请尝试使用CRLF行结束符:

"
"




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签