[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

how to write more than one text file to file

Posted on 2008-06-09
6
Medium Priority
?
228 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
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.

 

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

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!

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
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

650 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