Solved

opening a text file and writing contents to new filename

Posted on 2003-12-01
8
205 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
ID: 9856163
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
ID: 9860426
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
ID: 9862766
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Expert Comment

by:rjdown
ID: 9880436
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
ID: 9881648
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
ID: 12623933
PAQed with points refunded (125)

modulo
Community Support Moderator
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

911 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

22 Experts available now in Live!

Get 1:1 Help Now