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
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

 

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.