scan single directory - print filename and contents to page
Posted: Tue Apr 22, 2008 4:35 am
I need some help resolving some php code which needs to scan a directory (in this case, only 1 directory which can be named in the code if necessary) and prints each filename (minus its file suffix - in this case .txt) together with its contents, in 'date created/updated' order onto the screen.
I am using the following code:
This is giving an error message (the screen error says Line 39 - which is this line
This is just a test phase at the moment - the output can be viewed at: http://www.athenecreations.co.uk/cf/comments.php
Any help would be gratefully received - I am NON-TECHNICAL... so feel free to use plain english (please!)
Thanks
LouPhi
I am using the following code:
Code: Select all
<?php
$base = './'; // current dir
if($dr = opendir($base)){
while (($file = readdir($dr)) !== false) {
if(is_dir($base.$file) && preg_match("/^group1_content/", $file)){
echo "<h2>$file</h2>";
scan_Dir($base.$file);
};
};
closedir($dr);
};
function scan_Dir($dir){
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if(! is_dir($dir.$file)) {
echo '<div><p>'.preg_replace("/\.[^\.]*$/", '', $file) // check this line
."</p>\n<code>"
.nl2br(htmlspecialchars(file_get_contents($dir.$file)))
.'</code></div>';
};
};
closedir($dh);
}else
echo 'unable to open directory, check permissions';
}else
echo $dir.' is not seen as a directory';
};
?>
<?php
$dir = "comments/";
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if(! is_dir($dir.$file)) {
$file = preg_replace("/\.[^\.]*$/", '', $file);
echo "<div><p>$file</p>\n<code>"
.nl2br(htmlspecialchars(file_get_contents($dir.$file)))
.'</code></div>';
};
}
closedir($dh);
}else
echo 'unable to open directory, check permissions';
}else
echo $dir.' is not seen as a directory';
?>
<?php
$dir = "comments/";
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if(! is_dir($dir.$file)) {
$tmp = pathinfo($dir.$file);
echo "<div><p>{$tmp['filename']}</p>\n<code>"
.nl2br(htmlspecialchars(file_get_contents($dir.$file)))
.'</code></div>';
};
}
closedir($dh);
}else
echo 'unable to open directory, check permissions';
}else
echo $dir.' is not seen as a directory';
?> Removing this line from the code, gets rid of the error message, but the files are print to the screen incorrectly (the filenames are side by side, and the contents of the first file sits next to the second filename on the same line... with the contents of the second file starting below....nl2br(htmlspecialchars(file_get_contents($dir.$file)))
This is just a test phase at the moment - the output can be viewed at: http://www.athenecreations.co.uk/cf/comments.php
Any help would be gratefully received - I am NON-TECHNICAL... so feel free to use plain english (please!)
Thanks
LouPhi