markjoe Posted August 16, 2007 Share Posted August 16, 2007 Notice that Daniel0's function returns the value rather than echoing it. Which I think is a much better idea. But that means you need to "echo changeName($name)" rather than just calling it. Link to comment https://forums.phpfreaks.com/topic/65041-solved-listing-files-in-a-directory/page/2/#findComment-325484 Share on other sites More sharing options...
wrathican Posted August 16, 2007 Author Share Posted August 16, 2007 right i see. now i have a problem when using it though. i *think* im using it correctly heres the difference: before the function: after using the function: any idea why its doing this? heres the code im using when using the function: <?php function changeName($file) { $path_parts = pathinfo($file); return ucfirst(strtolower(str_replace(array('-', '_', ' '), ' ', $path_parts['filename']))); } $dirs = array(); $imgs = array(); $others = array(); $img_extensions = array(".gif", ".png", ".jpg", ".jpeg", ".jpe", ".bmp"); $files = scandir("."); foreach($files as $file) { if($file != "." && $file != "..") { $ext = strtolower(strrchr($file,".")); if(is_dir($file)) $dirs[] = $file; elseif(in_array($ext, $img_extensions)) $imgs[] = $file; else $others[] = $file; } } if(count($dirs) > 0) { echo "<b>Directories</b><ul>"; foreach ($dirs as $file) echo "<li><a href='".$file."'>"; echo changeName($file); echo "</a></li>"; echo "</ul>"; } if(count($imgs) > 0) { echo "<b>Images</b><ul>"; foreach ($imgs as $file) echo "<li><a href='".$file."'><img src='".$file."' width='25' border='0'> "; echo changeName($file); echo "</a></li>"; echo "</ul>"; } if(count($others) > 0) { echo "<b>Other Files</b><ul>"; foreach ($others as $file) echo "<li><a href='".$file."'>"; echo changeName($file); echo "</a></li>"; echo "</ul>"; } ?> Link to comment https://forums.phpfreaks.com/topic/65041-solved-listing-files-in-a-directory/page/2/#findComment-325549 Share on other sites More sharing options...
Orio Posted August 16, 2007 Share Posted August 16, 2007 EDIT- Read next post, not this!! I guess you are using PHP < 5.2.0 Use this function instead: <?php function changeName($file) { $file = substr($file, 0, -(strlen(strrchr($file, ".")))); return ucfirst(strtolower(str_replace(array('-', '_', ' '), ' ', $file))); } ?> Orio. Link to comment https://forums.phpfreaks.com/topic/65041-solved-listing-files-in-a-directory/page/2/#findComment-325555 Share on other sites More sharing options...
Orio Posted August 16, 2007 Share Posted August 16, 2007 lol I found the problem... Curly braces. When I orginally made the script each foreach had one line so I omitted the curly braces. But after you add more lines you have put them back. Do it this way: <?php function changeName($file) { $cut = -(strlen(strrchr($file, "."))); if($cut != 0) $file = substr($file, 0, $cut); return ucfirst(strtolower(str_replace(array('-', '_', ' '), ' ', $file))); } $dirs = array(); $imgs = array(); $others = array(); $img_extensions = array(".gif", ".png", ".jpg", ".jpeg", ".jpe", ".bmp"); $files = scandir("."); foreach($files as $file) { if($file != "." && $file != "..") { $ext = strtolower(strrchr($file,".")); if(is_dir($file)) $dirs[] = $file; elseif(in_array($ext, $img_extensions)) $imgs[] = $file; else $others[] = $file; } } if(count($dirs) > 0) { echo "<b>Directories</b><ul>"; foreach ($dirs as $file) { echo "<li><a href='".$file."'>"; echo changeName($file); echo "</a></li>"; } echo "</ul>"; } if(count($imgs) > 0) { echo "<b>Images</b><ul>"; foreach ($imgs as $file) { echo "<li><a href='".$file."'><img src='".$file."' width='25' border='0'> "; echo changeName($file); echo "</a></li>"; } echo "</ul>"; } if(count($others) > 0) { echo "<b>Other Files</b><ul>"; foreach ($others as $file) { echo "<li><a href='".$file."'>"; echo changeName($file); echo "</a></li>"; } echo "</ul>"; } ?> I also made it work with php < 5.2.0 Orio. Link to comment https://forums.phpfreaks.com/topic/65041-solved-listing-files-in-a-directory/page/2/#findComment-325558 Share on other sites More sharing options...
wrathican Posted August 16, 2007 Author Share Posted August 16, 2007 ohhh yeah! i did notice them last night when you first posted the code but being a n00b didnt think anything of it ahh now it works! thanks all for all your great help! thnks ever so much! Link to comment https://forums.phpfreaks.com/topic/65041-solved-listing-files-in-a-directory/page/2/#findComment-325561 Share on other sites More sharing options...
Orio Posted August 16, 2007 Share Posted August 16, 2007 No problem Enjoy. Orio. Link to comment https://forums.phpfreaks.com/topic/65041-solved-listing-files-in-a-directory/page/2/#findComment-325563 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.