Solved

a couple of file operations

Posted on 2002-03-31
5
243 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
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

828 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