 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
03-11-2008, 09:40 AM
|
#1 (permalink)
|
|
The Wanderer
Join Date: Feb 2008
Posts: 18
Thanks: 0
|
Read Directory Function
Most of you developing websites or having portfolios, this function may come in use in your PHP endeavors.
Example: Directory Quick Browse Script
Added Note: If you're using anything less than php version 5.0.2, or you're getting an error using the code, add this in as well.
PHP Code:
if(!defined('PHP_EOL'))
{
define('PHP_EOL', (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? "\r\n" : "\n"));
}
And here's the function :)
PHP Code:
function read_directory($directory)
{
if(is_dir($directory))
{
echo "<ul>" . PHP_EOL;
if($handle = @opendir($directory))
{
while(($file = readdir($handle)) !== false)
{
if($file != "." && $file != "..")
{
if(is_dir($directory . DIRECTORY_SEPARATOR . $file))
{
$directories[] = $file;
}
else
{
$files[] = $file;
}
}
}
if(!empty($directories))
{
sort($directories);
foreach($directories as $sub_directory)
{
echo "<li>" . $sub_directory . DIRECTORY_SEPARATOR . "</li>";
echo "<ul>";
read_directory($directory . DIRECTORY_SEPARATOR . $sub_directory);
echo "</ul>";
}
}
if(!empty($files))
{
sort($files);
foreach($files as $file)
{
echo "<li><a href=\"" . $directory . DIRECTORY_SEPARATOR . $file . "\">" . $file . "</a></li>" . PHP_EOL;
}
}
@closedir($handle);
echo "</ul>" . PHP_EOL;
}
}
}
To use it, just simply write the following code:
PHP Code:
read_directory('portfolio');
__________________
World domination would be much easier, if you would all just surrender to me already.
|
|
|
03-11-2008, 10:49 AM
|
#2 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Theres a better way to escape the two periods
PHP Code:
if ($file != ".") continue;
Only problem is you cannot do an else/elseif statement for that condition.
__________________
VillageIdiot can have my babbies ;d
|
|
|
|
03-11-2008, 10:51 AM
|
#3 (permalink)
|
|
The Wanderer
Join Date: Feb 2008
Posts: 18
Thanks: 0
|
I don't really see how that code is "better"..? It's pretty much the same thing..
__________________
World domination would be much easier, if you would all just surrender to me already.
|
|
|
03-11-2008, 10:53 AM
|
#4 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by CHASE
I don't really see how that code is "better"..? It's pretty much the same thing..
|
It's only one line of code xD
__________________
VillageIdiot can have my babbies ;d
|
|
|
|
03-11-2008, 10:57 AM
|
#5 (permalink)
|
|
The Wanderer
Join Date: Feb 2008
Posts: 18
Thanks: 0
|
As opposed to two ;) Not much of a difference, but thank you for the pointer :)
__________________
World domination would be much easier, if you would all just surrender to me already.
|
|
|
03-11-2008, 10:59 AM
|
#6 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by CHASE
As opposed to two ;) Not much of a difference, but thank you for the pointer :)
|
No problem, just trying to help. ^_^ and be a little snobby at the same time 
__________________
VillageIdiot can have my babbies ;d
|
|
|
|
03-11-2008, 11:06 AM
|
#7 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Just a friendly tip, check out the glob function 
__________________
|
|
|
|
03-11-2008, 11:08 AM
|
#8 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by Tanax
Just a friendly tip, check out the glob function 
|
I was thinking about that, but I didn't bother posting it xD at least, I thought Wildhoney >.> was going to post hehe
__________________
VillageIdiot can have my babbies ;d
|
|
|
|
03-11-2008, 03:14 PM
|
#9 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
Glob! I just fell in love with the glob function as soon as I stumbled across it.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
03-11-2008, 10:41 PM
|
#10 (permalink)
|
|
The Wanderer
Join Date: Feb 2008
Posts: 18
Thanks: 0
|
Glob will only return files of a certain extension, and supports only two arguments though.. This does an entire directory + sub directories :D
__________________
World domination would be much easier, if you would all just surrender to me already.
|
|
|
03-11-2008, 11:16 PM
|
#11 (permalink)
|
|
The Acquainted
Join Date: Feb 2008
Posts: 119
Thanks: 17
|
didn't want to create a new thread because the topic is really similar.
I was trying to make a recursive function just to count how many files are in a dir and it's subdirectories:
PHP Code:
<?php
$dir = "/var/www/html/";
$files = getfilesnum($dir);
echo $files." files in {$dir} and subdirs<br />";
closedir($dh);
function getfilesnum($d) { $dh = opendir($d); $files = 0; while ($f = readdir($dh)) { if (filetype($d.$f) == 'file') { $files++; } elseif ((filetype($d.$f) == 'dir') && ($f != '.') && ($f != '..')) { $files += getfilesnum($d.$f); } } return $files; }
?>
Isn't working :S it doesn't go recursive, don't know why. Any idea??
I saw that CHASE in his code put all the directories in a array and then called the recursive function, but it should work without using the array I think...
Aah I posted twice, please delete it admins :)
|
|
|
|
03-12-2008, 02:29 AM
|
#12 (permalink)
|
|
The Wanderer
Join Date: Feb 2008
Posts: 18
Thanks: 0
|
I would recommend using the function I built, and then just removing the echos, and having it $var++
That would probably do what you want :)
__________________
World domination would be much easier, if you would all just surrender to me already.
|
|
|
03-12-2008, 09:58 AM
|
#13 (permalink)
|
|
The Addict
Join Date: Nov 2007
Posts: 264
Thanks: 2
|
Not to be a stickler but ++$var is faster then $var++
__________________
"What everyone seems to forget is that while knowledge certainly is something - it's the implementation of knowledge that brings power" - Andres Galindo.
|
|
|
|
03-12-2008, 10:43 AM
|
#14 (permalink)
|
|
The Wanderer
Join Date: Feb 2008
Posts: 18
Thanks: 0
|
Oh cool, I didn't know that, thanks for the tip.
__________________
World domination would be much easier, if you would all just surrender to me already.
|
|
|
03-12-2008, 02:15 PM
|
#15 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Quote:
Originally Posted by TlcAndres
Not to be a stickler but ++$var is faster then $var++
|
By approximately 0.00000004 seconds each time. I'd say it's a moot point in this particular case when accessing the file system is the slowest operation occurring.
Quote:
Originally Posted by CHASE
Glob will only return files of a certain extension, and supports only two arguments though.. This does an entire directory + sub directories :D
|
glob will return whatever you tell it to return. If you just specify * as the pattern, glob will return all files and directories in the current working directory. It's simple enough to check for directories and go through each recursively, as you do in your own code.
|
|
|
|
03-12-2008, 04:08 PM
|
#16 (permalink)
|
|
The Acquainted
Join Date: Feb 2008
Posts: 119
Thanks: 17
|
Quote:
Originally Posted by CHASE
I would recommend using the function I built, and then just removing the echos, and having it $var++
That would probably do what you want :)
|
yes, but I want to use my code :)
Just want to know what is wrong there... 
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|