• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

opening a text file and writing contents to new filename

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
rexburgess
Asked:
rexburgess
1 Solution
 
lozlozCommented:
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
 
SnykeCommented:
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
 
rexburgessAuthor Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
rjdownCommented:
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
 
SnykeCommented:
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
 
moduloCommented:
PAQed with points refunded (125)

modulo
Community Support Moderator
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now