[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

file concatenation

Posted on 2004-10-27
14
Medium Priority
?
505 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
  • 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
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
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

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

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

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.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.
Suggested Courses

612 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