Jump to content

add data to text file?


muppet77

Recommended Posts

is there a php solution that will take

the raw data from one file and paste it

into another text file?

 

I have a master file and a download

file that always starts at quarter past

midnight . this data needs to be

pasted into the master file, in the

correct place , possibly overlapping

and replacing data that already exists.

 

the length of the download file will

vary but will always start at 00. 15

the two files are

 

www.maidenerleghweather.com/download.txt and also

www.maidenerleghweather.com/master.txt

 

any help appreciated .

Link to comment
https://forums.phpfreaks.com/topic/223759-add-data-to-text-file/
Share on other sites

Read data from one file

$dfile = "download.txt";
$dopr = fopen($dfile, 'r');
$collectdata= fread($dopr, 50000);
fclose($dopr);

write data into another file

$mfile = "master.txt";
$mopr = fopen($mfile, 'w') or die("could not open file");
fwrite($mopr, $collectdata);
fclose($mopr);

 

thanks, but will that write the data to the correct place?

 

eg in master you may have rows with data in them

1...

2...

3...

4...

5...

 

and in the download you may have

3...

4...

5...

6...

7...

 

 

i would like the 6th  and 7th row to be added and not the 3rd,4th or 5th as it is already present.

it is a bit difficult to implement with file management system. you need to use explode like functions and lots of coding is needed and you may get ambiguous results . alternatively you can do it easily with by using database even if writing to the file is important for you you can do it in short with the help of mysql or mssql database.

 

well this is my unsuccessful attempt so far.

 

what's going wrong?

 

<?php

 

$filename = "master.txt";

$fh = fopen($filename, 'r') or die("Couldn't open $filename");

 

while (!feof($fh))

{

  $wldata= fgets($fh);

  if (strpos($wldata,"Rain")!== FALSE)  //SKIP headers - "Rain" is found in both lines

    {}

  else

    {

    if (strpos($wldata,"0") !== FALSE) // ALWAYS a zero somewhere in the data

      {

      $wlarray = array() ;

      $wlarray = explode("\t",$wldata);

 

if (strtotime($wlarray[0]) > strtotime($lastdate))

        $lastdate = $wlarray[0];

if (strtotime($wlarray[0]) > strtotime($lastdate))

        $lasttime == $wlarray[1];     

      }

    }

}

fclose($fh);

 

print $lastdate; // this prints ok as a check

print $lasttime; //this doesn't seem to print as a check

 

 

 

 

$filename = "download.txt";

$fh = fopen($filename, 'a') or die("Couldn't open $filename");

 

while (!feof($fh))

{

  $wldata= fgets($fh);

  if (strpos($wldata,"Rain")!== FALSE)  //SKIP headers - "Rain" is found in both lines

    {}

  else

    {

    if (strpos($wldata,"0") !== FALSE) // ALWAYS a zero somewhere in the data

      {

      $wlarray = array() ;

      $wlarray = explode("\t",$wldata);

 

if (strtotime($wlarray[0]) >= strtotime($lastdate) && strtotime($wlarray[1]) >= strtotime($lasttime))

{

$myFile = "master.txt";

$fh = fopen($myFile, 'a') or die("can't open file");

$stringData = "$wlarray[0]\t";

fwrite($fh, $stringData);

$stringData = "$wlarray[1]\t";

fwrite($fh, $stringData);

$stringData = "$wlarray[2]\t";

fwrite($fh, $stringData);

$stringData = "$wlarray[3]\t";

fwrite($fh, $stringData);

$stringData = "$wlarray[4]\t";

fwrite($fh, $stringData);

$stringData = "$wlarray[5]\t";

fwrite($fh, $stringData);

$stringData = "$wlarray[6]\t";

fwrite($fh, $stringData);

$stringData = "$wlarray[7]\t";

fwrite($fh, $stringData);

$stringData = "$wlarray[8]\t";

fwrite($fh, $stringData);

$stringData = "$wlarray[10]\t";

fwrite($fh, $stringData);

$stringData = "$wlarray[11]\t";

fwrite($fh, $stringData);

$stringData = "$wlarray[12]\t";

fwrite($fh, $stringData);

$stringData = "$wlarray[13]\t";

fwrite($fh, $stringData);

$stringData = "$wlarray[14]\t";

fwrite($fh, $stringData);

$stringData = "$wlarray[15]\t";

fwrite($fh, $stringData);

$stringData = "$wlarray[16]\t";

fwrite($fh, $stringData);

$stringData = "$wlarray[17]\t";

fwrite($fh, $stringData);

$stringData = "$wlarray[18]\t";

fwrite($fh, $stringData);

$stringData = "$wlarray[20]\t";

fwrite($fh, $stringData);

$stringData = "$wlarray[21]\t";

fwrite($fh, $stringData);

$stringData = "$wlarray[22]\t";

fwrite($fh, $stringData);

$stringData = "$wlarray[23]\t";

fwrite($fh, $stringData);

$stringData = "$wlarray[24]\t";

fwrite($fh, $stringData);

$stringData = "$wlarray[25]\t";

fwrite($fh, $stringData);

$stringData = "$wlarray[26]\t";

fwrite($fh, $stringData);

$stringData = "$wlarray[27]\t";

fwrite($fh, $stringData);

$stringData = "$wlarray[28]\t";

fwrite($fh, $stringData);

$stringData = "$wlarray[29]\n";

fwrite($fh, $stringData);

fclose($fh);

}

 

 

     

      }

    }

}

fclose($fh);

 

 

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.