Scanning folders for files.

PHP coding talk.

Moderator: Moderators

Scanning folders for files.

Postby DoubleJ on Sat Apr 02, 2005 7:36 pm

How could I make a script that scans folders for files.

For instance:
I got a folder called:
Language
and within it are:
english.php
dutch.php
otherlanguage.php

now I need a script that will list these files (and if it was possible without the .php extension)
Any help would be apprecieted.
It never hurts to help.....
[ My phpBB Mods and Styles ]
My Mod: [ Simple Shoutbox Mod ]
[ DoubleJWeb ] [ Get Opera ]
User avatar
DoubleJ
Moderator
Moderator
 
Posts: 716
Joined: Sat Jan 22, 2005 9:58 pm
Location: DoubleJWeb.Net

Postby PostBot on Sat Apr 02, 2005 9:52 pm

Code: Select all
<?php

$res = opendir('Language');
while(($file = readdir($res)) !== false)
{
   $data = explode('.', $file);
   $ext = count($data) > 1 ? strtolower($data[count($data) - 1]) : '';
   if($ext === 'php')
   {
      echo substr($file, 0, strlen($file) - strlen($ext) - 1), '<br />';
   }
}
closedir($res);

?>
Do NOT pm me, I don't visit this forum anymore, don't own it, don't provide any support and don't moderate.
User avatar
PostBot
Moderator
Moderator
 
Posts: 10659
Joined: Sat Aug 02, 2003 3:52 pm
Location: Mars

Postby DoubleJ on Sun Feb 19, 2006 1:50 pm

I have now test this function since I use it in my download manager but now when I test it it returns the following file list:
.
..
blog.css
blog.php
core.php
default.css
doublejweb.prj
error.php
extension.inc
favicon.ico
javascript.js
link_out.php

and on empty folders it also returns the . and ..
now when I want to remove a dir I first need to delte all files but in the while loop I use it also looks at the . and .. so it returns a false since it cannot remove the files:
[code start="126" highlight="129-140"]
//
//before we are able to delete the folder we need to delete all files inside it.
//
$open_dir = opendir(DJ_FILE_PATH . $category_info['url_name']);
while(($file = readdir($open_dir)) !== false)
{
//
//remove the file.
//
if( !@unlink(DJ_FILE_PATH . $category_info['url_name'] . '/' .$file) )
{
message_die(GENERAL_ERROR, 'Failed to remove file: ' .$file);
}
}
closedir($open_dir);
//
//all files are removed, now we'll remove the dir.
//
if( !@rmdir(DJ_FILE_PATH . $category_info['url_name']) )
{
message_die(GENERAL_ERROR, 'Failed to delete folder: ' .$category_info['url_name']);
}
$message = 'Files deleted succesfully.';[/code]

What can I add so it does not return errors on the . and .. filenames inside the directory?
It never hurts to help.....
[ My phpBB Mods and Styles ]
My Mod: [ Simple Shoutbox Mod ]
[ DoubleJWeb ] [ Get Opera ]
User avatar
DoubleJ
Moderator
Moderator
 
Posts: 716
Joined: Sat Jan 22, 2005 9:58 pm
Location: DoubleJWeb.Net

Postby kvfb97 on Sun Feb 19, 2006 1:54 pm

Try adding this before the message_die...
Code: Select all
if($file !== '.' AND $file !== '..') {

Make sure you close it...
Tip: Don't argue with idiots, they'll drag you down to their level and beat you with experience.
User avatar
kvfb97
Registered User
Registered User
 
Posts: 1774
Joined: Mon Aug 02, 2004 8:18 am
Location: Northern Ireland, UK

Postby DoubleJ on Sun Feb 19, 2006 3:20 pm

Thanks, worked out well(spast, thought of way to complicated solutions....).
It never hurts to help.....
[ My phpBB Mods and Styles ]
My Mod: [ Simple Shoutbox Mod ]
[ DoubleJWeb ] [ Get Opera ]
User avatar
DoubleJ
Moderator
Moderator
 
Posts: 716
Joined: Sat Jan 22, 2005 9:58 pm
Location: DoubleJWeb.Net


Return to PHP Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron