Solved

Combine all files

Posted on 2016-09-07
6
48 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

734 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