Solved

how to write more than one text file to file

Posted on 2008-06-09
6
207 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

829 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