Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

a couple of file operations

What i am trying to do is get a list of the file in the current dir, by current i mean the one the script is running in appart form the script file its self "index.php", from this list an array i asume i want ot be anle to write the content of the files out to the web browser, eg dir called test 3 files index.php page1.txt page2.txt. The code i need will store page1.txt and page2.txt the pointers to them anyway in an array say files and then write the names of all the file out to the screen and then write the contens of the first one out to the screen, how is this done in php. thanks kieran
0
kplonk
Asked:
kplonk
  • 4
1 Solution
 
andrivCommented:
this works on UNIX

<?
//Get the current directory
$thisdir=opendir('.');

//Loop through each file
while($filename=readdir($thisdir))
{
//All except the current file & hiddne files
if(($filename != "index.php") && ($filname != ".") && ($filename != ".."))
{
//Open next file
$openfile=fopen($filename,"r")

//pass file data to browser
fpassthru($openfile);
}
}
}
?>
0
 
andrivCommented:
I did not test the script above you may have to change the code within the loop:

//Open next file
$openfile=fopen($filename,"r")

//pass file data to browser
fpassthru($openfile);


to:

$openfile=file($filename);
$openfile=implode($filename,"");
print("$openfile");

or some variation of it to print it to browser.

The loop will take you through each file in the directory.
0
 
andrivCommented:
I tested the one below and it works great:

<?

$directory=opendir(".");

while($myfile=readdir($directory))
{
if(is_file($myfile))
{
print("$myfile<br>");
$myfile2 = file($myfile);
$myfile2 = implode($myfile2,"\n");
print($myfile2);
}
}
0
 
kplonkAuthor Commented:
thanks mate gave me just what i needed.
0
 
andrivCommented:
No problem, glad I can help.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now