Link to home
Start Free TrialLog in
Avatar of gloriaewold41
gloriaewold41

asked on

merge files

Hi experts,

I need to merge 2 or more files. No matter the content the script should output lines from 2 or more files into one file.txt, randomized.

Thanks.
Avatar of Lee Wadwell
Lee Wadwell
Flag of Australia image

To merge known files ... this script will merge them into one, just place the file names into the $files array.  It does not randomize the contents however ... is that important?
<?php
error_reporting(E_ALL);
echo '<pre>';

// old files to be merged
$files   = array('/Apps/DATA/old_file_1.txt', '/Apps/DATA/old_file_2.txt');

// new file name
$newfile = '/Apps/DATA/new_file.txt';
// delete file if already exists
unlink($newfile);

foreach ( $files as $f ) {
    // Open the file to get existing content
    $data = file_get_contents($f);
    
    // Append the contents back to the new file
    file_put_contents($newfile, $data, FILE_APPEND);    
}
echo "done";
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gloriaewold41
gloriaewold41

ASKER

I need output data randomized.
My code randomizes the output - it shuffles (randomizes) the lines before writing them to the output file!
Works great. Thanks