Page 1 of 1

using file_put_contents

Posted: Wed Apr 11, 2007 10:34 am
by kingconnections
So basically I have this script that goes through a directory of log files and then opens them and parses them for specific data.

When it find the data it is suppose to open a log file and insert the entries into there.

That all works!! Yeah me!! But the problem is I am trying to add some headings and such. When i call the file_put_contents and try and do a /n or a /n/r it does not put an line return in there. Any ideas???


Here is my code:

Code: Select all

<?php
$valid_date=array("1/4/2007","1/9/2007","1/17/2007","1/22/2007","1/25/2007","1/30/2007","2/2/2007");
$d=dir('c:\scripts\logs');
while (false !== ($file = $d->read())){
if ($file === "." || $file === "..")
{
// do nothing
}
 else {
  $path2 = "c:\scripts\logs\\$file";
  echo $path2."\n";
  $test = file($path2);
  file_put_contents ('c:\scripts\sample.txt', "Data came from $file \n\r", FILE_APPEND);
  file_put_contents ('c:\scripts\sample.txt', "-------------------------------------".'\n\r', FILE_APPEND);
  foreach ($test as $item){
	if (in_array(rtrim(substr($item,0,9)), $valid_date)){ 
	echo $item." \n";
	file_put_contents ('c:\scripts\sample.txt', "$item", FILE_APPEND); 
	}
  }
 }
}
$d->close();
?>

Posted: Wed Apr 11, 2007 10:43 am
by volka
I can't reproduce the behaviour. newline/carriage-returns are written.

Sidenote: You should either keep the data in memory and write them in one (or at least larger blocks) via file_put_contents
or use fopen/fputs instead.

Posted: Wed Apr 11, 2007 10:45 am
by kingconnections
hmm strange! Did you open it in notepad, or wordpad? I am using notepad, maybe my notepad is hosed up.

Posted: Wed Apr 11, 2007 10:51 am
by kingconnections
ha notepad, did not work for me but wordpad did.


Thanks a ton volka!!!!

Posted: Wed Apr 11, 2007 10:56 am
by volka
I used ultraedit.


I just noticed
kingconnections wrote:"-------------------------------------".'\n\r'
shot yourself in the foot ;)
\r \n are only replaced in double quoted strings.



try

Code: Select all

$valid_date=array("1/4/2007","1/9/2007","1/17/2007","1/22/2007","1/25/2007","1/30/2007","2/2/2007");
$d=dir('c:\scripts\logs');

$fout = fopen('c:\scripts\sample.txt', 'a');
while (false !== ($file = $d->read())) {
  if ( $file !== "." && $file !== "..") {
    $path2 = "c:/scripts/logs/$file";
    echo $path2,  "\n";
    $test = file($path2);
    
    
    fputs($fout, "Data came from $file \r\n");
    fputs($fout, "-------------------------------------\r\n");
    foreach ($test as $item) {
      if (in_array(rtrim(substr($item,0,9)), $valid_date)){
        echo $item, " \n";
        fputs($fout, $item);
      }
    }
  }
}
$d->close();
fclose($fout);

Posted: Wed Apr 11, 2007 10:56 am
by nickvd
The problem is in your second file_put_contents...

Code: Select all

file_put_contents ('c:\scripts\sample.txt', "-------------------------------------".'\n\r', FILE_APPEND);
You're adding a ----- break and for some odd reason concatenating it with the newline enclosed within single quotes. I cant fathom why though :)

Code: Select all

file_put_contents ('c:\scripts\sample.txt', "-------------------------------------\n\r", FILE_APPEND);