Note: This script will read all .txt files in the directory you place this in, list them as links and will then create a page based on the variables provided. No database involved, written in PHP. If you have any questions please contact me at http://www.beckerist.com/ Second note: This page was built with wrapping turned off so you can see it easier. If you want the source in a cleaner format, contact me.
<?php
//dirpath is relative to your home directory, make sure you have read / browse access
$dirpath = "content/textfiles";
//show this if the pagename is not defined or is index
if ($pagename == '' || $pagename == 'index')
{
$dh = opendir($dirpath);
$x = 0;
//while reading is not 'false' loop through the contents of the directory
while (false !== ($file = readdir($dh)))
{
//if it exists, and is not index, throw into an array variable ($fulllist)
if (!is_dir("$dirpath/$file") && htmlspecialchars(preg_replace('/\..*$/', '', $file)) != "index")
{
//put the link right in the variable and remove the underscores in the filename for the link
$fulllist[$x] = '<a href="/content/textfiles/index.php?pagename=' .
htmlspecialchars(preg_replace('/\..*$/', '', $file)) . '">' .
ereg_replace("_", " ", htmlspecialchars(ucfirst(preg_replace('/\..*$/','', $file)))) . '</a><br>';
}
//increase the counter by 1
$x = $x + 1;
}
//sort the variable
sort($fulllist);
//output with linebreaks
foreach ($fulllist as $x => $val)
{
echo $val . "\n";
}
//close the connection
closedir($dh);
}
else
{
// some of this I ripped from php.net
$x = 0;
//read the raw text file as an array, the @ sign means it won't output any warnings to the screen (remove to troubleshoot)
$raw = @file('content/textfiles/' . $pagename . '.txt') or $x = 1;
if ($x == 1)
{
die("INVALID FILE. Try again.");
}
// join remaining data into string
$data = join('', $raw);
// replace special characters with HTML entities
// replace line breaks with <br />
$html = nl2br(htmlspecialchars($data));
// replace multiple spaces with single spaces
$html = preg_replace('/\s\s+/', ' ', $html);
//uncomment to replace URLs with <a href...> elements
//$html = preg_replace('/\s(\w+:\/\/)(\S+)/', ' <a href="\\1\\2" target="_blank">\\1\\2</a>', $html);
echo '<h2>File: ' . ucfirst(str_replace('_', ' ', $pagename)) . '</h2><br>
<a href="/content/textfiles/"><font size=1>back to file listing</font></a><br><br>';
//output the file contents
echo $html;
}
?>
|