Solved

how to write more than one text file to file

Posted on 2008-06-09
6
197 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
  • 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 125 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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 125 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
This article discusses how to create an extensible mechanism for linked drop downs.
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 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 …

706 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

19 Experts available now in Live!

Get 1:1 Help Now