Link to home
Create AccountLog in
Avatar of firekiller15
firekiller15

asked on

how to write more than one text file to file

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
Avatar of vibrazy
vibrazy
Flag of United Kingdom of Great Britain and Northern Ireland image

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
ASKER CERTIFIED SOLUTION
Avatar of vibrazy
vibrazy
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of firekiller15
firekiller15

ASKER

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?
 
$file1 ,2, 3 is variable use to call the filename set by user it can be any name user choose
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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?