Solved

how to write more than one text file to file

Posted on 2008-06-09
6
209 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 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 

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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
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 …

690 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