473,734 Members | 2,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting the filenames from a folder

<?php
$dirname = ".";
$dh = opendir($dirnam e);
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 1797
On Sun, 30 Oct 2005 15:10:42 -0000, Krustov <kr****@krustov .co.uk.INVALID>
wrote:
<?php
$dirname = ".";
$dh = opendir($dirnam e);
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.u k :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Oct 30 '05 #2
<comp.lang.ph p , Andy Hassall , an**@andyh.co.u k>
<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($som eDirectory, 'files');
$aListOfFfolder s = GetDirList($som eDirectory, 'folders');
function GetDirList($par ent, $filesorfolders ){
$oldDir = getcwd();
$list = array();

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

if ($filesorfolder s == "folders"){
if ( (is_dir($file)) && ($file != ".") && ($file !=
".."))
{$list[] = $file;
}}
if ($filesorfolder s == "files"){
if ( (is_file($file) ) && ($file != ".") && ($file !=
".."))
{$list[] = $file;
}}
}// while reading
closedir($handl e);
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,j pg,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
6451
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 site): Function MlsPicList(ByVal mls) path = MlsPath(mls) & "*.jpg" Dim fso Set fso = CreateObject("Scripting.FileSystemObject") parent = fso.GetParentFolderName(path)
5
17136
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
18571
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 directory called LABELMAKER. Everything is working fine. I just wanted to rename the directory and filenames. So I changed the directory name as "EASYLABEL" and also changed the filenames as EASYLABEL.SLN, EASYLABEL.VBPROJ and EASYLABEL.USER. So...
5
1312
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 code is below: Dim dir As New DirectoryInfo("C:\Test") Dim fileNames(dir.GetFiles("*.txt").Length - 1)) As String Dim myfile As FileInfo
0
1395
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 aspx and ascx filenames are changed to lowercase. The filenames are not being changed during the conversion process. After conversion completes, the filenames remain unchanged. However, the first time that the solution is opened in VS2005, the...
0
1417
by: ColdCoffee | last post by:
Scenario: My computer: name : PrasComp pass : Pras123 Domain : XYZ Computer I wanna access: Name : AnoComp pass : #Pras
0
2506
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 question is how do i iterate the file names with 5 filenames on each page? I have manage to retrieve and display the filenames with the codes below. Can anyone help me or give advice on how to do the iteration? The codes are below and able to...
6
3161
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 my listbox using folder browser control. A sample code would be great.
13
2810
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 used in multiple files, I have php files under / home/user/www/functions/. These files simply have So, in index.php and other files, I have
8
5155
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 our local server and add all these filenames to a particular field in the DB
0
8946
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8776
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9310
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9236
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.