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!
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!
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 Code:<?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);
}
?>
Corrections to my coding/thoughts welcome.
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!
A bit of a novice at PHP, I came up with this:
PHP Code:<?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();
?>
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
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
Corrections to my coding/thoughts welcome.
Fixed the issue! Thank you so much for your help bluewalrus and jscheuer1!
How about:
PHP Code:<?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();
?>
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
ajfmrf (08-16-2011)
That works just fine John.
Thanks for the quick fix
Bud
Bookmarks