Hi guys,

So today I am trying to do something that is probably quite simple. But my head is all over the place and I can barely think straight! Something which is not good for a newb!

I'm trying to display some text files on the web. These text files are all in their own seperate directory. I want to be able to list the directory. When you the file you want to view you are brought to a page which uses this code to display the text:

<?php 
$myFile = "directory/file.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo "<pre>$theData</pre>";
?>

The code I have to display the directory is:

<?php
$dir = "poems/"; 
if($scan = scandir($dir)){ 
	foreach ($scan as $value){ 
		if($value != "." && $value != ".."){
			echo $value."<br />";
		}
	}
}
?>

So in summary what I want is:
1. Using the second piece of code or similar display a list of contents.
2. Once you choose a link the page it displays using the first piece of code or similar.

I presume I should be using some kind of template solution to make my site run the way i want.

I'm sorry if I explained it all over the place but as I said im kind of all over the place at the moment! Thanks guys

Recommended Answers

All 8 Replies

Try this code. I have written but not tested it, hopefully it will work for you.

<?php


$dir="poems/";
printContents($dir);

if(isset($_REQUEST['f']))
{
	$fh = fopen($_REQUEST['g'],"r");
	$theData = fread($fh,filesize($_REQUEST['f']));
	fclose($fh);
	echo "<pre>$theData</pre>";
}

function printContents($directory)
{
                if($scan = scandir($directory))
	{
		foreach ($scan as $value)
		{
			if (is_dir($value) && $value!="." && $value!="..") {
				printContents($directory);
			}
			else if(is_file($value))
			{
				echo "<br/>Read contents for ";
                          echo "<a href=\"content_page.php?f=".$directory."/".$value."\">";
                          echo $directory."/".$value."</a>";
			}
		}
	}
}
?>

Hi Dasatti!

Thanks you very much for your help! Unfortunately the code isn't outputting anything. Can you please help shed some light on this? Thanks again!

Please try it now

<?php


$dir="poems/";
printContents($dir);

if(isset($_REQUEST['f']))
{
	$fh = fopen($_REQUEST['f'],"r");
	$theData = fread($fh,filesize($_REQUEST['f']));
	fclose($fh);
	echo "<pre>$theData</pre>";
}

function printContents($directory)
{
	
                if($scan = scandir($directory))
	{
		foreach ($scan as $value)
		{
			if (is_dir($value) && $value!="." && $value!="..") {
				printContents($directory);
			}
			else 
			{
				echo "<br/>Read contents for ";
                          echo "<a href=\"content_page.php?f=".$directory."/".$value."\">";
                          echo $directory."/".$value."</a>";
			}
		}
	}
}
?>

Dasatti thanks once again for you help. The code for outputting the directory list is working perfectly.

However there are 2 small problems. One it's outputting the dots at the top:

.
..

And the second when I click the link I get the following:

The requested URL /Site/content_page.php was not found on this server.

Thanks!

Hi there,

Here's the modified version of Dasatti's printContents:

<?php

$dir = "poems/";
printContents($dir);

if(isset($_REQUEST['f'])){
	$fh = fopen($_REQUEST['f'],"r");
	$theData = fread($fh,filesize($_REQUEST['f']));
	fclose($fh);
	echo "<pre>$theData</pre>";
}

function printContents($dir) {
    $mydir = opendir($dir);
    while(false !== ($file = readdir($mydir))) {
	if($file != "." && $file != "..") {
	    if(is_dir($dir.$file)) {
	        printContents($dir.$file) ; 
	    }
	    else{
	        echo "<br/>Read contents for ";
                echo "<a href=\"list.php?f=".$dir."/".$file."\">";
                echo $dir.$file."</a>";
	    }
	}
    }
    closedir($mydir);
}

?>

You probably forgot to change the target page content_page.php to the one which you are testing your code in. In my case, i was using a file code list.php, hence in the anchor tag i used list.php.

Either rename the page to content_page.php or change the href attribute in line no 28 to the name of the file to which you saved it with.

echo "<a href=\"<place_the_name_of_the_php_file_here>?f=".$directory."/".$value."\">";

Sorry I took so long in getting back guys, I was away for a bit there.

Anyways the code works beautifully so thank ye so much for the help!

You're welome.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.