Jump to content

Multiple files in fwrite?


napalm

Recommended Posts

is there any way to create/write not just one file but multiple in one fwrite?

 

so writing one file is like this yah?

$dir='dir/'; 
$myFile = ''.$dir.'file.txt';
$fh = fopen($myFile, 'x+') or die("can't open file");
$content = "content blah blah";
fwrite($fh, $content);

 

theres no way to possibly like separating in the fopen to multiple files with a ,?

$dir='dir/'; 
$myFile = ''.$dir.'file.txt'; $myFile2 = ''.$dir.'file2.txt';
$fh = fopen($myFile,$myFile2 'x+') or die("can't open file");
$content = "content blah blah";
fwrite($fh, $content);

 

or with ||?

 

$dir='dir/'; 
$myFile = ''.$dir.'file.txt'; $myFile2 = ''.$dir.'file2.txt';
$fh = fopen($myFile || $myFile2 'x+') or die("can't open file");
$content = "content blah blah";
fwrite($fh, $content);

 

any ideas? thanks

Link to comment
https://forums.phpfreaks.com/topic/206448-multiple-files-in-fwrite/
Share on other sites

[Gah,  Mchl beat me to it!]

 

Firstly:-

 

$myFile = ''.$dir.'file.txt';

 

can be done:-

 

$myFile = $dir."file.txt";

 

and fopen(FILE.txt, "r");//whatever mode you want

 

only takes one parameter/filename at a time, multiple opens needs multiple calls to fopen, at least thats my understanding of it, I usually put a ternary somewhere & check to see if one exist && if not open the other, kinda like a failsafe if you see what I mean :)

 

Rw

 

What's wrong with opening two handles?

 

<?php
$dir='dir/'; 
$myFile1 = $dir.'file.txt'; $myFile2 = $dir.'file2.txt';
$fh1 = fopen($myFile1,'x+') or die("can't open file $myFile1");
$fh2 = fopen($myFile2,'x+') or die("can't open file $myFile2");
$content = "content blah blah";
fwrite($fh1, $content);
fwrite($fh2, $content);

Sorry for the double post but i got this figured out!

 

what i did is i opened the directory and listed each directory and just added the fwrite an what not where the list goes like this:

 

<?php
$dirname = "dir/";
$dirread = opendir($dirname);
while(false != ($file = readdir($dirread)))
{
if(($file != ".") and ($file != ".."))
{
$dir='dir/'.$file.''; 
$myFile = ''.$dir.'.txt';
$fh = fopen($myFile, 'x+') or die("can't open file");
$content = "content blah blah";
fwrite($fh, $content);
fclose($fh);
}}
?>

 

an it worked this way! Just for any one wondering :D

 

Thanks guys!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.