Solved

Combine all files

Posted on 2016-09-07
6
30 Views
Last Modified: 2016-09-07
How can I combine all the files I have in a directory?
I want them to be all copied in a document to be called "combined.txt"
0
Comment
Question by:Jasmine Ikhreishi
  • 3
6 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 41787760
cat $(ls -t) > combined.txt

That's a Linux command. Wrap it in PHP and you're set.

HTH,
Dan
0
 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41787761
Yes but the first one has certain criteria, while this one is any file in the directory
0
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41787803
Then just remove the criteria - make it *.*

So for the first sample
<?php
$output = 'combined.txt';
$files = glob("*.*");
unlink($output);
foreach($files as $f) {
    file_put_contents($output, file_get_contents($f), FILE_APPEND);
}

Open in new window

Second option adapted from (Ray's Solution)
$input = 'path/to/input';
$output = 'path/to/combine.txt';
$iterator = new FilesystemIterator($input, FilesystemIterator::SKIP_DOTS + FilesystemIterator::CURRENT_AS_PATHNAME);
unlink($output);
foreach ($iterator as $key => $filepath)
{
    file_put_contents($output, file_get_contents($filepath), FILE_APPEND);
}

Open in new window


EDIT:
The Second solution is about 4x faster than the first.
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41787813
Corrected code - there were errors in the post above
$iterator = new FilesystemIterator($input, FilesystemIterator::SKIP_DOTS + FilesystemIterator::CURRENT_AS_PATHNAME);
@unlink($output);
foreach ($iterator as $key => $filepath)
{
  file_put_contents($output, file_get_contents($filepath), FILE_APPEND);
}

Open in new window

0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41787816
And in case there are folders in the input directory
$iterator = new FilesystemIterator($input, FilesystemIterator::SKIP_DOTS + FilesystemIterator::CURRENT_AS_PATHNAME);
@unlink($output);
foreach ($iterator as $key => $filepath)
{
  if (!is_dir($filepath)) {
    file_put_contents($output, file_get_contents($filepath), FILE_APPEND);
  }
}

Open in new window

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now