473,395 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Listing only certain files in directory

2
[Windows XP pro sp2, running abyss webserver]

Ok, I'm stuck.
I'd like to list only specific types of files in a directory - for example, only html files.
I've found a script that will list ALL of the files in the directory, but I'd like to limit it to only files of a certain type:
[code]
<?php
$myDirectory = opendir(".");

while($entryName = readdir($myDirectory)) {
$dirArray[] = $entryName;
}

closedir($myDirectory);

$indexCount = count($dirArray);
Print ("$indexCount files<br>\n");

sort($dirArray);

print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\n");
print("<TR><TH>Filename</TH><th>Filetype</th><th>Filesize</th></TR>\n");
for($index=0; $index < $indexCount; $index++) {
if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files
print("<TR><TD><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>");
print("<td>");
print(filetype($dirArray[$index]));
print("</td>");
print("<td>");
print(filesize($dirArray[$index]));
print("</td>");
print("</TR>\n");
}
}
print("</TABLE>\n");

?>
[/CODE[

Does anyone have any ideas/suggestions?
Jan 16 '08 #1
3 2941
nathj
938 Expert 512MB
[Windows XP pro sp2, running abyss webserver]

Ok, I'm stuck.
I'd like to list only specific types of files in a directory - for example, only html files.
I've found a script that will list ALL of the files in the directory, but I'd like to limit it to only files of a certain type:
[php]
<?php
$myDirectory = opendir(&quot;.&quot;);

while($entryName = readdir($myDirectory)) {
$dirArray[] = $entryName;
}

closedir($myDirectory);

$indexCount = count($dirArray);
Print (&quot;$indexCount files<br>\n&quot;);

sort($dirArray);

print(&quot;<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\n&quot;);
print(&quot;<TR><TH>Filename</TH><th>Filetype</th><th>Filesize</th></TR>\n&quot;);
for($index=0; $index < $indexCount; $index++) {
if (substr(&quot;$dirArray[$index]&quot;, 0, 1) != &quot;.&quot;){ // don't list hidden files
print(&quot;<TR><TD><a href=\&quot;$dirArray[$index]\&quot;>$dirArray[$index]</a></td>&quot;);
print(&quot;<td>&quot;);
print(filetype($dirArray[$index]));
print(&quot;</td>&quot;);
print(&quot;<td>&quot;);
print(filesize($dirArray[$index]));
print(&quot;</td>&quot;);
print(&quot;</TR>\n&quot;);
}
}
print(&quot;</TABLE>\n&quot;);

?>
[/php]

Does anyone have any ideas/suggestions?
Hi,

Looking over the code supplied I noticed that you know what the file type:
[php]
filetype($dirArray[$index]) ;
[/php]
So where the code checks to make sure the file is not hidden you could simply expand that check to ensure the file type matches what you are after.

The downside with the option is that yyou get all files regardless and then only use some. This may be a good or a bad thing.


So you could issue something like:
[php]
if (substr(&quot;$dirArray[$index]&quot;, 0, 1) != &quot;.&quot; AND filetype($dirArray[$index] == &quot;html&quot;)
{
// carry on
}
[/php]
This would only display files where the type was html. You can see how this can be modified. If there is more than one valid file type then you ould store the valid ones in an array and then search the array as part of the if condition.

I hope this helps
nathj
Jan 16 '08 #2
galf
2
Hey! thanks for the speedy reply.

I realized I was being too lazy about this, and if I didn't figure something out on my own I would never learn to code effective php.
Because there's only one filetype (other than the main index.php that does the parsing) in the directory, I decided to ditch the code I listed above for something like this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. echo "<table>";
  3. $files = scandir(".");
  4. foreach ($files as $key => $value)
  5. if ($value == "index.php" ) {
  6. echo "$null";
  7. }
  8. elseif ($value == ".") {
  9. echo $null;
  10. }
  11. elseif ($value == "..") {
  12. echo $null;
  13. }
  14. else {
  15. echo "<td><a href=$value>$value</a></td>";
  16. }
  17. echo "</table>";
  18. ?>
  19.  
I'm pretty impressed with this - mostly because it's my first code and it went through with very few corrections, but if you have any ideas on how to improve upon that, it would be great!

Thanks again for your help, and I think I'm going to keep the first code in storage somewhere (with your tips) as something to play around with in future.
Jan 16 '08 #3
nathj
938 Expert 512MB
Hey! thanks for the speedy reply.

I realized I was being too lazy about this, and if I didn't figure something out on my own I would never learn to code effective php.
Because there's only one filetype (other than the main index.php that does the parsing) in the directory, I decided to ditch the code I listed above for something like this:
[php]
<?php
echo &quot;<table>&quot;;
$files = scandir(&quot;.&quot;);
foreach ($files as $key => $value)
if ($value == &quot;index.php&quot; ) {
echo &quot;$null&quot;;
}
elseif ($value == &quot;.&quot;) {
echo $null;
}
elseif ($value == &quot;..&quot;) {
echo $null;
}
else {
echo &quot;<td><a href=$value>$value</a></td>&quot;;
}
echo &quot;</table>&quot;;
?>
[/php]

I'm pretty impressed with this - mostly because it's my first code and it went through with very few corrections, but if you have any ideas on how to improve upon that, it would be great!

Thanks again for your help, and I think I'm going to keep the first code in storage somewhere (with your tips) as something to play around with in future.
Hi,

That looks pretty good, and you're right of course the best way to learn is to have a go.

You could try changing the if elseif structure for a switch. It would be a bit easier to maintain. Alternatively a negative IF would do the trick:
[php]
echo &quot;<table>&quot;;
$files = scandir(&quot;.&quot;);
foreach ($files as $key => $value)
{
if((! $value == 'index.php') AND (!$value=='.') AND (!$value=='..'))
echo &quot;<td><a href=$value>$value</a></td>&quot;;
}
}
[/php]

Of course if it's now working then ther eis no real reason to change anything.

Cheers
nathj
Jan 16 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Kim Jensen | last post by:
I'd like to make a directory listing where instead of the entire filename I need it to show the filename minus the extention and get the value of charname= in the file itself. I've been told...
3
by: David Callaghan | last post by:
Hi I've just joined tesco broadband. I've come from NTL. If I don't put an index.htm on my NTL home page it justs lists the files in there when any browser visites my page. If I don't...
19
by: SU News Server | last post by:
I've struggled with this for quite a while and I'm am just not sure what is going on. I have the following code import os def buildList( directory='/Users/mkonrad' ) dirs = listing =...
3
by: David Jacques | last post by:
I am trying to get a list of all files of a certain extension type on disk to do some processing in a loop. The code needs to be portable to UNIX, so I need to use plain c functionality. Does...
8
by: gil | last post by:
Is it possible to prevent a browser from listing the entire contents of a folder? The site, is hosted on my ISP with the following layout- site/ "user name from ISP" pagefile (dir)...
8
by: dougawells | last post by:
Hi - I'm hoping for help with the auto-generation of a hyperlinked listing of all files in a directory. The server I use does not auto-generate this. So, when someone comes to this directory and...
3
by: dougawells | last post by:
Hi - I'm hoping for help with the auto-generation of a hyperlinked listing of all files in a directory. The server I use does not auto-generate this. So, when someone comes to this directory and...
7
by: epikto | last post by:
I have a mapped share that I am trying to get a listing of all the files that it contains. I use the following code to access the contents String files = Directory.GetFiles(path); I can then...
5
by: jain236 | last post by:
HI every body, i am always getting the following error while parsing a directory . i am reading a directory by doing ls and trying to find out the name,type,size, mtime and mode of files from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.