Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

file concatenation

Posted on 2004-10-27
14
Medium Priority
?
500 Views
Last Modified: 2008-02-01
I have two files that I wish to concatenate with the following code and I am sure I am not approaching this correctly...

$myDirectory = opendir($pathtodata);
      if (!$myDirectory) $mailBody .= "Failed to open $pathtodata for reading\n";
while ($filename = readdir($myDirectory)) {
 if ($filename == "$ListingData.P" && $banfilename=="bancroft.sql"){
   $myfile = fopen("$pathtodata$filename", 'r+');
   $mybanfile = fopen("$pathtodata$banfilename", 'r+');
   if (!$myfile) {
     $mailBody .= "failed to open properties file for editing\n";
     exit;
    } // is read KO
      else if (!$mybanfile) {
     $mailBody .= "failed to open bancroft file for editing\n";
     exit;
    } // is read KO
    else { // let's go

      // prepare for writing
      $newfn="properties.sql";
      $newfile=fopen($newfn,'a+');
      if (!$newfile) $mailBody .= "failed to open properties.sql for editing\n";
      // get first line
      // get subsequent and write them
      $str=file("$pathtodata$filename");
      for ($i=1;$i<count($str);$i++) fputs($newfile,$str[$i]); // effectively ignoring first line (index=0)
      // concatenation will start here.
      // get subsequent and write them
      $banstr=file("$pathtodata$banfilename");
      for ($x=1;$x<count($banstr);$x++) fputs($newfile,$banstr[$x]); // effectively ignoring first line (index=0)
      // close everything
      fclose($myfile);
      fclose($mybanfile);
      fclose($newfile);
      $mailBody .= "Properties dump file created\n";
0
Comment
Question by:frankenstrat
[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
  • 6
  • 4
  • 3
  • +1
14 Comments
 
LVL 3

Expert Comment

by:KvdnBerg
ID: 12424836
instead of reading the file line by line in an array using file, you could try fread which returns the entire file as a string. also keep in mind that fputs is just an alias of fwrite so you might as well use fwrite, which is easier to read in combination with fread.
0
 
LVL 1

Expert Comment

by:steinmr1
ID: 12424840
Your code is a bit bloated, but in short, here is what I think you need to do

fopen the file1 (the file you want to add to file2) with read permission.

fopen file2 (the file your want to add to) with append permission
and for each line of file1, write it to file2.

OR you could open a third file and write file1 and file2 to it.

Hope this helps
0
 

Author Comment

by:frankenstrat
ID: 12424860
I need to remove the first line of each file prior to concatenating them and this is where i am getting lost.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Expert Comment

by:KvdnBerg
ID: 12424921
Also, it might be a better idea to put both files in one string and writing to the file only once:

$file1 = fread($myfile);
$file2 = fread($mybanfile);

$complete_file = $file1 . $file2;

fwrite($newfile, $complete_file);

This instead of the part from //get first line until //close everything
0
 
LVL 1

Expert Comment

by:steinmr1
ID: 12424932
to remove the first line of file1 is easy, just skip it when reading. (Set a flag when first line is read)
To remove the first line of file2, you have to create file3

fopen(file1,r)
fopen(file2,r)
fopen(file3,w)

firstlinefile1=false;
foreach line in file1 {
       if(!firstlinefile1) { firstlinefile1=true; continue
        fwrite(getline(file1),file3)
}

firstlinefile2=false;
foreach line in file2 {
       if(!firstlinefile2) { firstlinefile2=true; continue
        fwrite(getline(file2),file3)
}

close all files


hope this helps
0
 
LVL 3

Expert Comment

by:KvdnBerg
ID: 12424955
Ok, to remove the first line:
$file1 = fread($myfile);
$file2 = fread($mybanfile);

$secondlinepos1 = strpos($file1,"\n");
$secondlinepos2 = strpos($file2,"\n");

$file1 = substr($file1, $secondlinepos1);
$file2 = substr($file2, $secondlinepos2);

$complete_file = $file1 . $file2;

fwrite($newfile, $complete_file);

Give me a minute to test this...
0
 
LVL 1

Expert Comment

by:steinmr1
ID: 12424999
Not trying to be negative, but reading a file into a string is not a very good idea, unless you _know_ that the files are going to be small.
0
 
LVL 3

Expert Comment

by:KvdnBerg
ID: 12425068
Ok, that might be true, but just in case, here's the working code

   $myfile = fopen("$pathtodata$filename", 'r+');
   $mybanfile = fopen("$pathtodata$banfilename", 'r+');

$file1 = fread($myfile, filesize("file1.txt"));
$file2 = fread($mybanfile, filesize("file2.txt"));

$secondlinepos1 = strpos($file1,"\n") + 1;
$secondlinepos2 = strpos($file2,"\n") + 1;

$file1 = substr($file1, $secondlinepos1);
$file2 = substr($file2, $secondlinepos2);

$complete_file = $file1 . "\r\n" . $file2;

     $newfn="properties.sql";
     $newfile=fopen($newfn,'a+');

fwrite($newfile, $complete_file);
0
 

Author Comment

by:frankenstrat
ID: 12425906
Good point  steinmr1, the files are quite large and I would like to avoid problems. Any suggestions?
0
 
LVL 1

Expert Comment

by:steinmr1
ID: 12426061
If you just use mye pseudocode, and replace it with real functions from php.net, you will have the solution.

I can type it out for you, but then you'd have to wait until tomorrow, and I bet you'll learn a bunch by doing it yourself ;-)
0
 

Author Comment

by:frankenstrat
ID: 12426630
steinmr1 I am a little lost on the pseudocode, if you could expand upon it, it would be greatly appreciated.
0
 
LVL 1

Expert Comment

by:steinmr1
ID: 12426778
Suro, will post it tomorrow (08 GMT)
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 12427068
When filesize stays below approximately 8mb you could use the next snippet

<?php
  $output_file = 'file1_and_2.txt';
  $input_file_1 = 'file1.txt';
  $input_file_2 = 'file2.txt';
  //open outputstream
  $out = fopen($output_file, 'w');
  //read file 1
  $in = file($input_file1);
  //write file 1
  foreach($in as $line)
   fwrite($out, $line);

  //.. possibly a fwrite($out, "\n"); //at this line

  //read file 2
  $in = file($input_file2);
  //write file 2
  foreach($in as $line)
   fwrite($out, $line);

  //close output stream
  fclose($out);
?>

Regards

-r-
0
 
LVL 1

Accepted Solution

by:
steinmr1 earned 2000 total points
ID: 12431032
Here you go. Need a lot of error checking ofcourse, but it works....


$filestocon=array("file1","file2");
// Open file for writing
$af=fopen("file3",w);

foreach($filestocon as $filename) {
  $f=fopen($filename,r);
  if(!feof($f)) fgets($f); // Skip first line
   // Add contents to f3
   while (!feof($f)) fwrite($af,fgets($f));
   fclose($f);
}
fclose($af);
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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.
Suggested Courses

636 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