Read multiple files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PunjabHaker
Forum Newbie
Posts: 2
Joined: Sun Dec 31, 2006 4:47 pm

Read multiple files

Post by PunjabHaker »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


[b]passwords.txt contain:[/b]

Code: Select all

user1 pass1
user2 pass2
sites.txt contain:

Code: Select all

site1
site2
Code:

Code: Select all

<?php
$handle = fopen("passwords.txt", "r");
$lines = file('sites.txt');
foreach ($lines as $line_num => $site) {
while ($userinfo = fscanf($handle, "%s\t%s")) {
list ($user, $pass) = $userinfo;
print "$site : $user : $pass \n";
}
}
?>
I need it to show me like that:

Code: Select all

site1 : user1 : pass1
site1 : user2 : pass2
site2 : user1 : pass1
site2 : user2 : pass2
Question: Where i'm wrong ?[/b]


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What is your code currently doing?

Have you considered/tried using file() on "passwords.txt"?

explode() may be of interest too.
PunjabHaker
Forum Newbie
Posts: 2
Joined: Sun Dec 31, 2006 4:47 pm

Post by PunjabHaker »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I tried this one:

Code: Select all

<?php
$oks = fopen("oks.txt", "a");
$handle = fopen("passwords.txt", "r");
$lines = file('sites.txt');
foreach ($lines as $site) {
    while ($userinfo = fscanf($handle, "%s %s")) {
        list ($user, $pass) = $userinfo;
        fwrite($oks, "$site : $user : $pass\r\n");
    }
    rewind($handle);
}
fclose ($handle);
?>
But it shows me:

Code: Select all

site1
 : user1 : pass1
site1
 : user2 : pass2
site2 : user1 : pass1
site2 : user2 : pass2
How can i make it to show site1 like site2 ?
Thanks in advance


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

file() doesn't remove line breaks, so you can use trim() to do so.
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

May We Know Why You Are doing this in a txt file
you can do it in a MySQL Databases
And using a txt file is not that much secure
You Can also do it very easily with xml
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

Well you can Use This One
===================

Code: Select all

<?php
$fw = fopen("sites.txt", "rb+") or die("Couldn't open File");
while(!feof($fw))
  {
    fflush($fw);
    $sln = fgets($fw, 1024);
    $arr_sln = explode("\r", $sln);
    $p_w[] = $arr_sln[0];
  }
$fp = fopen("passwords.txt", "rb+") or die("Couldn't open File");
while(!feof($fp))
  {
    fflush($fp);
    $gln = fgets($fp, 1024);
    $arr_gln = explode("\r", $gln);
    $p_p[] = $arr_gln[0];
  }
for($i=0;$i<=count($arr_sln);$i++)
  {
    $apnd_file[] = $p_w[$i]." ".$p_p[$i]."\r";
  }
$fa = fopen("res.txt", "wb+") or die("Couldn't Open File");//Here You Can Use ab+ If You Need
fflush($fa);
for($i=0;$i<=count($apnd_file);$i++)
  {
    fputs($fa, $apnd_file[$i]."\n");
  }
fclose($fw);
fclose($fp);
fclose($fa);
?>
It Makesw Its Out put On res.txt as
=========================
res.txt wrote:site1 user1 pass1
site2 user2 pass2
With files
=======
passwords.txt wrote:user1 pass1
user2 pass2
and
sites.txt wrote:site1
site2
Post Reply