Solved

opening a text file and writing contents to new filename

Posted on 2003-12-01
8
192 Views
Last Modified: 2008-03-06
I need to open up a text file and then write the contents of that text file into a different file. Inside the text file there are some variables that need the values printed out in the new file. The Original file is merely a template and the other files have certain filenames changed accordingly. Does that make sens? I hope so.

--Rex
0
Comment
Question by:rexburgess
8 Comments
 
LVL 13

Expert Comment

by:lozloz
Comment Utility
hi,

i don't quite understand what you're trying to do but i suppose it's something like this:

<?php
$readfilename = "file.txt"; // change this
$writefilename = "fileout.txt"; // change this too
$file = file_get_contents($readfilename);
eval("\$file = \"$file\";");
$fp = fopen($writefilename, "wb");
if(!$fp) {
  print "Error in opening file";
} else {
  $write = fwrite($fp, $file);
  if($write) {
    print "Successfully written to file";
  } else {
    print "Error writing to file";
  }
}
?>

tell me how you get on

loz
0
 
LVL 1

Expert Comment

by:Snyke
Comment Utility
Try doing this:
<?php
  $readfilename = "file.txt";
  $writefilename = "fileout.txt";
  $fd = fopen($writefile) or die("Could not open file.");
  $indata = file($readfile) or die("Could not open file.");
  foreach($indata as $curline){
    $outdata[] = preg_replace("/%([^ ]+)%/",$indata,$${1}); // <-- Notice the 2 $$ are wanted.
    fwrite($fd,$outdata);
  }
?>
Notice that I've never tried to do the $$ stuff but I heard about it some time ago and ou seem to need it.
Otherwise just define a preg_replace for every variable you want to insert in the file.
0
 

Author Comment

by:rexburgess
Comment Utility
Alright, this is what I came up with and it works like a charm.

---------------------------------------------------------------------------
lot.php  script
---------------------------------------------------------------------------

<?PHP
include("global.php");

//open template file
$contents=file_get_contents("$full_file_path/lot.txt");

//replace variables
$contents2=ereg_replace("VARIABLE_LOT_NUMBER", "$lot", "$contents");
$filename_template=ereg_replace("VARIABLE_PLAN_NUMBER", "$plan", "$contents2");

//create new file
touch("$full_file_path/lot$lot_number.html");

//chmod new file
chmod("$full_file_path/lot$lot_number.html", 0777);

//open new file for writing
$filename_final=fopen("$full_file_path/lot$lot_number.html", "w")
  or die("<center>ERROR: Cannot open desired lot. Please try again later.</center>");
 
//write to new file
fputs($filename_final,$filename_template);
?>

---------------------------------------------------------------------------
end  lot.php  script
---------------------------------------------------------------------------


--Rex
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 6

Expert Comment

by:rjdown
Comment Utility
Might I just add a note here:

according to the php manual:

"preg_replace(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to ereg_replace()."

Furthermore, str_replace() should be used in this case. Once again from the PHP manual:

"If you don't need fancy replacing rules, you should always use this function instead of ereg_replace() or preg_replace()."

I'm being fussy I know, but good habits make good scripts...
0
 
LVL 1

Expert Comment

by:Snyke
Comment Utility
Yes that's right.
But I noticed that doing it your way, rexburgess, replaces just one kind of strings. If you have more than one substitutions defined in lot.txt you will have to do a foreach that cycles through the substitutions.
0
 

Accepted Solution

by:
modulo earned 0 total points
Comment Utility
PAQed with points refunded (125)

modulo
Community Support Moderator
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now