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

}

Link to comment
Share on other sites

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

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.