muppet77 Posted January 8, 2011 Share Posted January 8, 2011 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 . Quote Link to comment Share on other sites More sharing options...
inversesoft123 Posted January 8, 2011 Share Posted January 8, 2011 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); Quote Link to comment Share on other sites More sharing options...
muppet77 Posted January 8, 2011 Author Share Posted January 8, 2011 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. Quote Link to comment Share on other sites More sharing options...
inversesoft123 Posted January 8, 2011 Share Posted January 8, 2011 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. Quote Link to comment Share on other sites More sharing options...
muppet77 Posted January 8, 2011 Author Share Posted January 8, 2011 how about this as an idea but not sure how I'd code it... look at the master file and get the last date and time. loop through the download file. if date and time is later than these values then write that line to the master file. any ideas? Quote Link to comment Share on other sites More sharing options...
muppet77 Posted January 9, 2011 Author Share Posted January 9, 2011 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); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.