Rifts Posted August 4, 2010 Share Posted August 4, 2010 Hey! I have a bunch of text files with one line of text ex. SHS9GW34 I'm trying to write a little php script on my local machine that will take all the files and put all the different codes in the same files in this kind of format: RGDGIDSG453 EOHBDSBN453 ERGD4535355 etc etc Thanks Quote Link to comment https://forums.phpfreaks.com/topic/209738-merging-text-files/ Share on other sites More sharing options...
trink Posted August 4, 2010 Share Posted August 4, 2010 <?php $x = array( file_get_contents('file1'), file_get_contents('file2'), file_get_contents('file3') ); file_write_contents('final_file', implode("\n", $x)); ?> That should do the trick. Quote Link to comment https://forums.phpfreaks.com/topic/209738-merging-text-files/#findComment-1094895 Share on other sites More sharing options...
Rifts Posted August 4, 2010 Author Share Posted August 4, 2010 how to i browse to find the files though Quote Link to comment https://forums.phpfreaks.com/topic/209738-merging-text-files/#findComment-1094902 Share on other sites More sharing options...
Rifts Posted August 4, 2010 Author Share Posted August 4, 2010 anyone Quote Link to comment https://forums.phpfreaks.com/topic/209738-merging-text-files/#findComment-1094933 Share on other sites More sharing options...
PFMaBiSmAd Posted August 4, 2010 Share Posted August 4, 2010 You are not providing any information in your post that would pin down what is needed. Where these files are located (are they all in one folder or spread out over 1000's of folders that are inside of a common folder) and what are their names (do they have a common naming format, do they have a common extension?) Quote Link to comment https://forums.phpfreaks.com/topic/209738-merging-text-files/#findComment-1094934 Share on other sites More sharing options...
Rifts Posted August 4, 2010 Author Share Posted August 4, 2010 sorry they are all in the same directory as my index.php page. they are all .txt files and the names are pretty random but if there is a way just to use ANY file that ends it .txt that would work best. also this is the error im currently getting: Fatal error: Call to undefined function file_write_contents() in C:\wamp\www\test\index.php on line 7 using this code: <?php $x = array( file_get_contents('file1.txt'), file_get_contents('file2.txt'), file_get_contents('file3.txt') ); file_write_contents('final_file.txt', implode("\n", $x)); ?> i have the three files in the directory as index currectly and line 7 is file_write_contents('final_file.txt', implode("\n", $x)); Quote Link to comment https://forums.phpfreaks.com/topic/209738-merging-text-files/#findComment-1094951 Share on other sites More sharing options...
AbraCadaver Posted August 4, 2010 Share Posted August 4, 2010 sorry they are all in the same directory as my index.php page. they are all .txt files and the names are pretty random but if there is a way just to use ANY file that ends it .txt that would work best. also this is the error im currently getting: Fatal error: Call to undefined function file_write_contents() in C:\wamp\www\test\index.php on line 7 using this code: <?php $x = array( file_get_contents('file1.txt'), file_get_contents('file2.txt'), file_get_contents('file3.txt') ); file_write_contents('final_file.txt', implode("\n", $x)); ?> i have the three files in the directory as index currectly and line 7 is file_write_contents('final_file.txt', implode("\n", $x)); Yeah, because there is no such function, and there is no reason to build and implode() and array: $data = ''; foreach(glob('*.txt') as $file) { $data .= file_get_contents($file); } file_put_contents('newfile.txt', $data); Quote Link to comment https://forums.phpfreaks.com/topic/209738-merging-text-files/#findComment-1095079 Share on other sites More sharing options...
DavidAM Posted August 4, 2010 Share Posted August 4, 2010 put all the different codes in the same files in this kind of format: 1) If there is a possibility that there are duplicate codes and you don't want the duplicates, you may want to look at array_unique() 2) I'd be careful naming the file 'newfile.txt' -- If you run the script a second time, the glob() call will include this file and you will double your data. Either give it a different extension, or delete it (unlink()) before the call to glob(), or specifically exclude it within the loop. Quote Link to comment https://forums.phpfreaks.com/topic/209738-merging-text-files/#findComment-1095083 Share on other sites More sharing options...
trink Posted August 5, 2010 Share Posted August 5, 2010 I definitely just noticed my error there, it would be file_put_contents, my bad. Ok try this <?php $newfile = 'result.txt'; $files = glob('C:\path\to\files\*.txt'); foreach($files as $file) file_put_contents($newfile, file_get_contents($file) . "\n", FILE_APPEND); ?> EDIT: Just noticed that AbraCadaver posted essentially the same code, and his would be better than mine, seeing as how his incurs only one filewrite, and mine incurs multiple. The more you know. Quote Link to comment https://forums.phpfreaks.com/topic/209738-merging-text-files/#findComment-1095388 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.