Solved

a couple of file operations

Posted on 2002-03-31
5
250 Views
Last Modified: 2012-03-15
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
Comment
Question by:kplonk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 5

Accepted Solution

by:
andriv earned 50 total points
ID: 6909336
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
 
LVL 5

Expert Comment

by:andriv
ID: 6909362
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
 
LVL 5

Expert Comment

by:andriv
ID: 6909440
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
 

Author Comment

by:kplonk
ID: 6910732
thanks mate gave me just what i needed.
0
 
LVL 5

Expert Comment

by:andriv
ID: 6910745
No problem, glad I can help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with mod_substitute 18 76
curl parse data from site 20 64
&& operators not performing required logic 8 40
How to open a new browser tab after executing php script 20 48
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question