Solved

a couple of file operations

Posted on 2002-03-31
5
211 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks mate gave me just what i needed.
0
 
LVL 5

Expert Comment

by:andriv
Comment Utility
No problem, glad I can help.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now