Solved

Combine all files

Posted on 2016-09-07
6
50 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
6 Comments
 
LVL 35

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 57

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 57

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 57

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

717 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