?
Solved

how to write more than one text file to file

Posted on 2008-06-09
6
Medium Priority
?
220 Views
Last Modified: 2008-07-05
PHP
i have this

 $file1 = 'test1';
 $file2 = 'test2';
 $file3 = 'test3';

 $fp = fopen('c:\\'.$file1.'.txt', 'w');
 $fp = fopen('c:\\'.$file2.'.txt', 'w');
 $fp = fopen('c:\\'.$file3.'.txt', 'w');


$headertofile = array ('a', 'b', 'c', 'd');
$fileheader = implode("|",$headertofile);
fwrite($fp, $fileheader . "\r\n");
fclose($fp);

why this code able to output file3.txt only
0
Comment
Question by:firekiller15
[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
  • 3
  • 2
6 Comments
 
LVL 5

Expert Comment

by:vibrazy
ID: 21748913
Hi,

You are assigning $fp to all the fopens, and the $fp = fopen('c:\\'.$file3.'.txt', 'w');
is the last one, so its the one being used.

Try assigning different name variable $fp, $fp_a, $fp_2.

That might work.

Regards,
Vibrazy
0
 
LVL 5

Accepted Solution

by:
vibrazy earned 375 total points
ID: 21748957
That should work for you
<?php
$string = "test";
for($i=1;$i<=3;$i++)
{
	$fp = fopen($string.$i.'.txt', 'w');
	$headertofile = array ('a', 'b', 'c', 'd');
	$fileheader = implode("|",$headertofile); 
	fwrite($fp, $fileheader . "\r\n");
	fclose($fp);
}
?>

Open in new window

0
 

Author Comment

by:firekiller15
ID: 21749026
How about if
**config.php**
$file1 = 'test1';
 $file2 = 'test2';
 $file3 = 'test3';
 $fp = fopen('c:\\'.$file1.'.txt', 'w');
 $fp = fopen('c:\\'.$file2.'.txt', 'w');
 $fp = fopen('c:\\'.$file3.'.txt', 'w');

if from config file

i use include to call config.php

include(config.php)
$headertofile = array ('a', 'b', 'c', 'd');
$fileheader = implode("|",$headertofile);
fwrite($fp, $fileheader . "\r\n");
fclose($fp);
how?
 
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:firekiller15
ID: 21749037
$file1 ,2, 3 is variable use to call the filename set by user it can be any name user choose
0
 
LVL 8

Assisted Solution

by:MrEne
MrEne earned 375 total points
ID: 21749067
you may use file_put_contents (http://hu2.php.net/file_put_contents) instead, if you use PHP5.

file_put_contents ( 'a.txt' , 'a' );
file_put_contents ( 'b.txt' , 'b' );
file_put_contents ( 'c.txt' , 'c' );
0
 

Author Comment

by:firekiller15
ID: 21749124
but i want w mode and not file_append mode how?

furthermore above seems like not optimize cause more that one file_put_contents
what happen if i want to add more that 100 different file name?
can optimize code above?
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

764 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