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
i have this
$file1 = 'test1';
$file2 = 'test2';
$file3 = 'test3';
$fp = fopen('c:\\'.$file1.'.txt'
$fp = fopen('c:\\'.$file2.'.txt'
$fp = fopen('c:\\'.$file3.'.txt'
$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
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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?
**config.php**
$file1 = 'test1';
$file2 = 'test2';
$file3 = 'test3';
$fp = fopen('c:\\'.$file1.'.txt'
$fp = fopen('c:\\'.$file2.'.txt'
$fp = fopen('c:\\'.$file3.'.txt'
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?
ASKER
$file1 ,2, 3 is variable use to call the filename set by user it can be any name user choose
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
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?
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?
You are assigning $fp to all the fopens, and the $fp = fopen('c:\\'.$file3.'.txt'
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