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
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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);

 

 

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.