Shoutbox

PHP File Indexer - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: PHP File Indexer (/showthread.php?tid=39331)

PHP File Indexer by DJeX on 03-01-2005 at 12:42 AM

How could I take this code and output the result to a txt file on the server?

code:
<?php
$maindir = "." ;
$mydir = opendir($maindir) ;
$exclude = array( "index.php") ;
while($fn = readdir($mydir)) { if ($fn == $exclude[0] || $fn == $exclude[1]) continue;
echo "<br><a href='$fn'>$fn</a>"; } closedir($mydir);
?>


What it does is it lists all the files in that folder that the indexer.php is in.
So how would I take that list and put it in a txt file?

If any one knows of a simpler or better way of outputting all the file names and the links to them in a txt file on the server please feel free to post it.
RE: PHP File Indexer by L. Coyote on 03-01-2005 at 12:45 AM

You can use the output buffer functions. :o

Just before the while(), put ob_start() and in the end of the loop, put $tofile = ob_get_contents();

And that goes to the file. You know, fopen() and fwrite()?

:P I'm feeling lazy, sorry.

code:
ob_start();
// while loop
$text = ob_get_contents();
$op = fopen($file,'w'); // or 'a' if you want to append it
fwrite($op, $text);
fclose($op);


I find that easier than putting variables all over the place.
RE: PHP File Indexer by TheBlasphemer on 03-01-2005 at 12:55 AM

how about putting fopen at the start,
fclose at the end, and replace the echo by fwrite?


RE: PHP File Indexer by DJeX on 03-01-2005 at 01:04 AM

Ive tryed this code

code:
<?php
$maindir = "." ;
$mydir = opendir($maindir) ;
$exclude = array( "index.php") ;
while($fn = readdir($mydir)) { if ($fn == $exclude[0] || $fn == $exclude[1]) continue;
$file = fopen("file.txt","w");
fwrite($file,"<br><a href='$fn'>$fn</a>"); } closedir($mydir);
fclose($file);
?>


But alls it wrote in the txt was:

code:
<br><a href='indexer.php'>indexer.php</a>

RE: PHP File Indexer by TheBlasphemer on 03-01-2005 at 01:07 AM

quote:
Originally posted by DJeX
Ive tryed this code

code:
<?php
$maindir = "." ;
$mydir = opendir($maindir) ;
$exclude = array( "index.php") ;
while($fn = readdir($mydir)) { if ($fn == $exclude[0] || $fn == $exclude[1]) continue;
$file = fopen("file.txt","w");
fwrite($file,"<br><a href='$fn'>$fn</a>"); } closedir($mydir);
fclose($file);
?>


But alls it wrote in the txt was:

code:
<br><a href='indexer.php'>indexer.php</a>



put the fopen BEFORE the while!
RE: PHP File Indexer by DJeX on 03-01-2005 at 01:08 AM

lol ok thanks it worked