multi file / multi tasked php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
trauch
Forum Newbie
Posts: 14
Joined: Thu Dec 11, 2008 4:50 pm

multi file / multi tasked php

Post by trauch »

So as to not confuse anyone regarding my progress I have started a new thread.

I'm working on a script that will perform a few tasks
1) Load multiple text files each of which contains a line of numbers
2) In each text file remove white space and then add all lines
3) Append the contents of all of these text files to another text file
4) Add all numbers in the resulting text file
5) Save that resulting number as a new text file whose name is generated as the current date. (ie 2008-12-22.txt)

I've got some messy code below but would appreciate any assistance.

Code: Select all

<?php
 
$data1 = file('textfile1.txt');
$data1 = str_replace(' ','',$data1);
echo 'sum($data1) = ' . array_sum($data1) . "\n";
$sum = array_sum($data1) . "\n";
 
$data2 = file('textfile2.txt');
$data2 = str_replace(' ','',$data2);
echo 'sum($data2) = ' . array_sum($data2) . "\n";
$sum2 = array_sum($data2) . "\n";
 
$data3 = file('textfile3.txt');
$data3 = str_replace(' ','',$data3);
echo 'sum($data3) = ' . array_sum($data3) . "\n";
$sum3 = array_sum($data3) . "\n";
 
 
$fp = fopen('textmaster.txt', 'a');
fwrite($fp, $sum);
fwrite($fp, $sum2);
fwrite($fp, $sum3);
 
fclose($fp);
 
?>
trauch
Forum Newbie
Posts: 14
Joined: Thu Dec 11, 2008 4:50 pm

Re: multi file / multi tasked php

Post by trauch »

With the help of others I'm almost there but I would appreciate any input. Since Im using php4 I cant use file_put_contents and am still trying to figure out how to save the file as the current date (2008-12-22.txt). I know I have to use fwrite but am not sure how to do this.

Here's what I have thus far.

Code: Select all

<?php
// Make an array of the filenames to sum
$fileNames = array('textfile.txt', 'textfile2.txt', 'textfile3.txt');
 
// Open the file and zero out the net sum
$fp = fopen('textmaster.txt', 'a');
$netSum = 0;
 
// Add the numbers and append to the file
foreach($fileNames as $fileName) {
   $content = str_replace(' ', '', file($fileName)); 
   $sum = array_sum($content);
   echo 'Sum of "' . $fileName . ': ' . $sum;
   $netSum += $sum;
   fwrite($fp, $sum);
}
 
// Clean up
fclose($fp);
 
// Create the day file
fwrite(date('Y-n-j') . '.txt', $netSum);
?>
maneetpuri
Forum Commoner
Posts: 60
Joined: Tue Oct 07, 2008 6:32 am

Re: multi file / multi tasked php

Post by maneetpuri »

trauch wrote:So as to not confuse anyone regarding my progress I have started a new thread.

I'm working on a script that will perform a few tasks
1) Load multiple text files each of which contains a line of numbers
2) In each text file remove white space and then add all lines
3) Append the contents of all of these text files to another text file
4) Add all numbers in the resulting text file
5) Save that resulting number as a new text file whose name is generated as the current date. (ie 2008-12-22.txt)

I've got some messy code below but would appreciate any assistance.

Code: Select all

<?php
 
$data1 = file('textfile1.txt');
$data1 = str_replace(' ','',$data1);
echo 'sum($data1) = ' . array_sum($data1) . "\n";
$sum = array_sum($data1) . "\n";
 
$data2 = file('textfile2.txt');
$data2 = str_replace(' ','',$data2);
echo 'sum($data2) = ' . array_sum($data2) . "\n";
$sum2 = array_sum($data2) . "\n";
 
$data3 = file('textfile3.txt');
$data3 = str_replace(' ','',$data3);
echo 'sum($data3) = ' . array_sum($data3) . "\n";
$sum3 = array_sum($data3) . "\n";
 
 
$fp = fopen('textmaster.txt', 'a');
fwrite($fp, $sum);
fwrite($fp, $sum2);
fwrite($fp, $sum3);
 
fclose($fp);
 
?>

Hi,
I would say that you should re-write it using the logic below: -

~ construct name of the file to which the final output has be saved.
~ open this file in the write mode.
~ Now run a while loop in which you will be opening all the text files one by one, read the content, remove spaces and then write it to the file to which you have first create the object.
~ After the while loop close the file object of the final output.

Hope this helps.

Cheers,

~Maneet
Post Reply