473,396 Members | 1,784 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,396 software developers and data experts.

Getting the filenames from a folder

<?php
$dirname = ".";
$dh = opendir($dirname);
while ($file = readdir($dh))
{
if (is_dir ("$dirname/$file"))
{
print "";
}
print "$file<br>";
}
closedir ($dh);
?>
..
...
aaa.html
etc

The above code brings up the single and twin full stops - presumably the
option to change directorys or whatever if wanted .

Got the code snippet via google and just wondered if the above code is
the best way it could be written .
Oct 30 '05 #1
5 1787
On Sun, 30 Oct 2005 15:10:42 -0000, Krustov <kr****@krustov.co.uk.INVALID>
wrote:
<?php
$dirname = ".";
$dh = opendir($dirname);
while ($file = readdir($dh))
{
if (is_dir ("$dirname/$file"))
{
print "";
}
print "$file<br>";
}
closedir ($dh);
?>
.
..
aaa.html
etc

The above code brings up the single and twin full stops - presumably the
option to change directorys or whatever if wanted .

Got the code snippet via google and just wondered if the above code is
the best way it could be written .


See the example in the manual, it's superior in a couple of ways.

http://uk.php.net/opendir

For example, consider the behaviour of the code you posted in the presence of
files or directories named "0".
--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Oct 30 '05 #2
<comp.lang.php , Andy Hassall , an**@andyh.co.uk>
<tj********************************@4ax.com>
<Sun, 30 Oct 2005 15:29:18 +0000>
Got the code snippet via google and just wondered if the above code is
the best way it could be written .


See the example in the manual, it's superior in a couple of ways.

http://uk.php.net/opendir


Fair enough - just thought I would ask before I started messing around
with it .
Oct 30 '05 #3
I usually use glob() 'cause I'm lazy.

http://fi.php.net/glob/

Oct 30 '05 #4

Chung Leong wrote:
I usually use glob() 'cause I'm lazy.

http://fi.php.net/glob/


nice, i never saw that. I assume you could go *.*, can you give it
multiple matches, like *.jpg, *.gif ?

here is a func which can give an array of either folders or files:

$aListOfFiles = GetDirList($someDirectory, 'files');
$aListOfFfolders = GetDirList($someDirectory, 'folders');
function GetDirList($parent, $filesorfolders){
$oldDir = getcwd();
$list = array();

if ($handle = opendir($parent)) {
chdir($parent);
while (false !== ($file = readdir($handle))) {

if ($filesorfolders == "folders"){
if ( (is_dir($file)) && ($file != ".") && ($file !=
".."))
{$list[] = $file;
}}
if ($filesorfolders == "files"){
if ( (is_file($file)) && ($file != ".") && ($file !=
".."))
{$list[] = $file;
}}
}// while reading
closedir($handle);
chdir($oldDir);
}//if handle

sort($list);
reset($list);
return $list;
} //func

Oct 31 '05 #5

juglesh wrote:
Chung Leong wrote:
I usually use glob() 'cause I'm lazy.

http://fi.php.net/glob/


nice, i never saw that. I assume you could go *.*, can you give it
multiple matches, like *.jpg, *.gif ?


Yes, if you pass the GLOB_BRACE flag.

Example:

$images = glob("*.{jpeg,jpg,gif,png}", GLOB_BRACE);

You can also character class matching with glob():

$images = glob("[ab]*.gif"); // gif files starting with letter a or b

And wildcards work for sub-folder as well:

$images = glob("*/*.gif"); // gif files in all sub-folders

A real time-saver. One downside though is that the function is broken
in some versions on Win32.

Oct 31 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Dave Navarro | last post by:
I have a folder on the server with several thousand image files in it. I am using the following code to retrieve the image names associated with a given MLS number (this is for a real estate web...
5
by: Mark | last post by:
How can I populate a listbox with a list of the filenames in a certain folder? Can a query retrieve the filenames from a folder? Thanks! Mark
6
by: I_AM_DON_AND_YOU? | last post by:
I have a small VB.Net Solution. The solution has filename LABELMAKER.SLN and the the project has filename LABELMAKER.VBPROJ. There is another file LABELMAKER.VBPROJ.USER. All these are in a...
5
by: Amjad | last post by:
Hi, I want to write a For loop that will put file names of extensions (*.txt and *.csv) in an array. I had it work fine for one extension, and I need help making it work for two extensions. My...
0
by: n33470 | last post by:
We have a web site that is being converted from the 1.1 format into 2.0. I've noticed that after the web project has been converted, the first time that the solution is opened in VS, all of the...
0
by: ColdCoffee | last post by:
Scenario: My computer: name : PrasComp pass : Pras123 Domain : XYZ Computer I wanna access: Name : AnoComp pass : #Pras
0
by: chongming | last post by:
Hi, i want to display all the filenames on browser. However i found that if there are many filenames in that folder, result will be it will display a long list of filenames on that browser. My...
6
by: kimiraikkonen | last post by:
Hello, I have a listbox and folder browser control. I need to display all .mp3 files' pathes into listbox. It was Ok with openfiledialog but how can list all .mp3 extension- having files into...
13
by: lawpoop | last post by:
Hello all - I have a two part question. First of all, I have a website under /home/user/www/. The index.php and all the other website pages are under /home/user/www/. For functions that are...
8
colinod
by: colinod | last post by:
does anyone know if its possible to add all the filenames in a folder to an access database i.e. a folder contains file1.mp3 file2.mp3 file3.mp3 i want a code that will search a folder on...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.