Jump to content

[b]How to merge all textfiles in a folder to one text file using php[/b]


php1

Recommended Posts

<?php

$path	 = "includes/sites/centre/test/";				// Directory containing files
$newFile  = "includes/sites/centre/test/saturn.php";			// Path to new file
$fType	 = strtolower(substr($newFile, strpos($newFile, ".")));	// File type of files to collate (got automatically)
$file	 = @scandir($path);

if ($file)
{
echo "Copying all $fType files from '<b>$path</b>' to '<b>$newFile</b>'<br /><br />";

foreach($file as $file)
{
	if (strlen($file) < 3)					continue;
	if (strtolower(substr($file, strpos($file, "."))) != $fType)	continue;
	if (strtolower($path.$file) == strtolower($newFile))		continue;

	$f  = fopen($path.$file, "r");

	if ($f)
	{
		$buff	= fread($f, filesize($path.$file));
		$f	= fclose($f);


		$f	= fopen($newFile, "a");

		if ($f)
		{
			fwrite($f, $buff);
			$f = fclose($f);
		}	
	}
}
}

?>

  • 1 month later...

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.