Jump to content

Reading data from a bunch of files and adding them to one file


alphazulu

Recommended Posts

Ok, So, This has been on my nerves for a few days now

 

The point of this is not too actually write to a file, but while I am testing I have it just writing to a file, later on it will edit a Wordpress page, and some other stuff

 

First it reads from a file and gets a list of other files, next it opens a file, stores it in an array and should append it to a file, then repeat, adding the contents of each file to the end of the file.

However, all it is doing is taking the contents of the first file and putting that intp the file.

Here is the code, what am I missing? I feel like its right in front of me..

$master = "master.txt";
$mlines = count(file($master));
$mlines = $mlines - 1;
$cat = array();
$files = array();

$ah = fopen($master, 'r');
$temp = fgets($ah);
$cat = explode("=", $temp);
fclose($ah);

for($b = 0; $b <= $mlines; $b++){
$fh = fopen($cat[$b], 'r');
	$lines = count(file($cat[$b]));
	$lines = $lines - 1;

	for($c = 0; $c <= $lines; $c++){
		$files[$c] = fgets($fh);
		lp_post($files[$c]);
	}
fclose($fh);
}


function lp_post($files){

$text = "text.txt";
$h = fopen($text, 'a');
	fwrite($h, $files);
fclose($h);

}

try something like this:

 

$allText = '';
$masterFile = file_get_contents('master.txt');
$filenames = explode("\n",$masterFile);
foreach($filenames as $file){
$allText .= "\n".get_file_contents($file);
}
file_put_contents('text.txt',$allText);

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.