Solved

Combine all files

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

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 54

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 54

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

776 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