Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

a couple of file operations

Posted on 2002-03-31
5
Medium Priority
?
258 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 200 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

670 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