View Full Version : Display Directory Contents Without Extentions Included
M2com
08-03-2011, 12:33 AM
Is there a way to display all the file in a directory but not have the file extentions included? For Example:
Directory Contents
file
file2
NOT
Directory Contents
file.php
file2.html
Thanks!
bluewalrus
08-03-2011, 01:12 AM
Yup, http://php.net/manual/en/function.readdir.php
If you will only ever have one period in your file name this should (untested) work, actually this may work if there is more than 1 period as well.
<?php
$directory = '/path/to/files';
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$file = preg_replace('/(.*)\..*/', '$1', $file);
echo "$file\n";
}
}
closedir($handle);
}
?>
M2com
08-03-2011, 01:35 AM
Hmm. What I'm using to test my site is local host and when I put your script into my page, it returns with: All Users Default Default User desktop Mark Public TEMP
What happened and what did I do wrong (if anything) and how to fix it.
Thanks!
jscheuer1
08-03-2011, 01:41 AM
A bit of a novice at PHP, I came up with this:
<?php
function listnoext(){
if($handle = opendir('.')){
$pattern = '#\.[^\.]+$#';
clearstatcache();
while(false !== ($file = readdir($handle))){
$files[] = preg_replace($pattern, '', $file);
}
closedir($handle);
}
echo implode('<br>', $files);
}
echo 'Directory Contents:<br>';
listnoext();
?>
bluewalrus
08-03-2011, 02:50 AM
It sounds like you outputted the wrong directory. Were the files you want in any of those folders?
All Users Default Default User desktop Mark Public TEMP
M2com
08-09-2011, 03:24 PM
Fixed the issue! Thank you so much for your help bluewalrus and jscheuer1!
ajfmrf
08-16-2011, 01:11 AM
A bit of a novice at PHP, I came up with this:
<?php
function listnoext(){
if($handle = opendir('.')){
$pattern = '#\.[^\.]+$#';
clearstatcache();
while(false !== ($file = readdir($handle))){
$files[] = preg_replace($pattern, '', $file);
}
closedir($handle);
}
echo implode('<br>', $files);
}
echo 'Directory Contents:<br>';
listnoext();
?>
This works the best so far but I was wondering if the list could somehow show the directories in a way they can easily be seen( as to making them easily distingished from other file types.)
_pardon my spelling please.
bud
jscheuer1
08-16-2011, 03:49 AM
How about:
<?php
function listnoext(){
if($handle = opendir('.')){
$pattern = '#\.[^\.]+$#';
clearstatcache();
while(false !== ($file = readdir($handle))){
if(is_dir($file)){
$files[] = '<span style="color: red;">' . preg_replace($pattern, '', $file) . '</span>';
} else {
$files[] = preg_replace($pattern, '', $file);
}
}
closedir($handle);
}
echo implode('<br>', $files);
}
echo 'Directory Contents:<br>';
listnoext();
?>
ajfmrf
08-16-2011, 03:40 PM
That works just fine John.
Thanks for the quick fix
Bud
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.