473,407 Members | 2,359 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,407 software developers and data experts.

how to read file names within a folder?whats wrong with my code

116 100+
how can i get the php to read the all the file names within a folder and put the names into an array?
Jun 25 '10 #1

✓ answered by code green

Found it
Expand|Select|Wrap|Line Numbers
  1. public static function getFiles($folder,$ignore='')
  2. {
  3.     $filenames = false;
  4.     // Remove any trailing slash
  5.     if(substr($folder, -1) == '/')
  6.         $folder = substr($folder, 0, -1);
  7.  
  8.     //Can't remember why all these tests are here
  9.     if(file_exists($folder) || !is_dir($folder) || !is_readable($folder))
  10.     {
  11.         // Grab a file handle
  12.         if($handle = opendir($folder))
  13.         {
  14.             $filenames = array();
  15.             while(false !== ($file = readdir($handle)))
  16.             {
  17.                 //Ignore folders in the directory
  18.                 if(!is_dir($folder.'/'.$file))
  19.                 {
  20.                     //Skip DOS shortcuts
  21.                     if(!(!strcmp($file,'.') || !strcmp($file,'..')))
  22.                     {
  23.                         //Skip extensions in ignore
  24.                         if(!empty($ignore))
  25.                         {
  26.                             foreach($ignore as $ext)
  27.                             {
  28.                                 if(strpos($file,$ext)===false)
  29.                                         $filenames[] = $file;
  30.                             }
  31.                         }
  32.                         else $filenames[] = $file;
  33.                     }
  34.                 }
  35.             }
  36.         }
  37.         else throw new Exception('Could not open '.$folder);
  38.     }
  39.     else throw new Exception('Folder does not exist '.$folder);
  40.  
  41.     closedir($handle);
  42.     sort($filenames);
  43.  
  44.     return $filenames;
  45. }

6 2385
code green
1,726 Expert 1GB
If you can set the folder up as an FTP location then ftp_nlist() does exactly this.
Otherwise.. I think I have done this somewhere!? Will try to find relevant code
Jun 25 '10 #2
code green
1,726 Expert 1GB
Found it
Expand|Select|Wrap|Line Numbers
  1. public static function getFiles($folder,$ignore='')
  2. {
  3.     $filenames = false;
  4.     // Remove any trailing slash
  5.     if(substr($folder, -1) == '/')
  6.         $folder = substr($folder, 0, -1);
  7.  
  8.     //Can't remember why all these tests are here
  9.     if(file_exists($folder) || !is_dir($folder) || !is_readable($folder))
  10.     {
  11.         // Grab a file handle
  12.         if($handle = opendir($folder))
  13.         {
  14.             $filenames = array();
  15.             while(false !== ($file = readdir($handle)))
  16.             {
  17.                 //Ignore folders in the directory
  18.                 if(!is_dir($folder.'/'.$file))
  19.                 {
  20.                     //Skip DOS shortcuts
  21.                     if(!(!strcmp($file,'.') || !strcmp($file,'..')))
  22.                     {
  23.                         //Skip extensions in ignore
  24.                         if(!empty($ignore))
  25.                         {
  26.                             foreach($ignore as $ext)
  27.                             {
  28.                                 if(strpos($file,$ext)===false)
  29.                                         $filenames[] = $file;
  30.                             }
  31.                         }
  32.                         else $filenames[] = $file;
  33.                     }
  34.                 }
  35.             }
  36.         }
  37.         else throw new Exception('Could not open '.$folder);
  38.     }
  39.     else throw new Exception('Folder does not exist '.$folder);
  40.  
  41.     closedir($handle);
  42.     sort($filenames);
  43.  
  44.     return $filenames;
  45. }
Jun 25 '10 #3
angelicdevil
116 100+
altering as the code i put here was wrong and i figured the answer myself
Jun 25 '10 #4
code green
1,726 Expert 1GB
but i m getting error please help
Difficult to fix an error when you don't tell people what it is
Jun 25 '10 #5
angelicdevil
116 100+
@code green
yes codegreen my mistake but initally i had had the part displaying the title and body for single file when i put the $filename directly ...but i wanted to broaden the horizen and make rewrite for all the files within the directory by reading it..but its giving me the problem then.but thanx for the code i did use parts of it .
Jun 26 '10 #6
angelicdevil
116 100+
ok no need ...i figured out the problem and sorted it out too...thanx
Jun 26 '10 #7

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

Similar topics

1
by: Stein | last post by:
I've seen a couple postings on moving files to folders with set source and destination paths, but I still need a little help taking it a step further. I have a folder with 2,000 Word docs that I...
3
by: MS | last post by:
I would like to be able to examine a folder, and populate a List Box or Combo Box with the files that are in it. How would you go about doing this? The reason is that I would like the user to...
9
by: Paul | last post by:
I'm trying to make get my app to delete all the files in a specified folder and all the files within the folders of the specified folder. e.g. Folder 1 contains three files (File1, File2, File3)...
6
by: Mark | last post by:
I want the VB.Net program to read and file names from a directory. I thought the best thing to do was to place these file names into a "temporary" MS Access database for sorting, etc. So I'm...
2
by: JIM.H. | last post by:
Hello, I need to read all the files in current and sub folders and put it into a list box, is there any example in C# I can use?
4
by: thumper5 | last post by:
i searched these forums for a file counting snippet of code to count the number of files in folder and return a value but i couldn't find one that worked. i just want to have it search the folder...
6
by: rlntemp-gng | last post by:
I need to extract information from some Excel files but am stuck with part of it: As an example, I have the following Excel File that has 3 tabbed sheets: FileName: ...
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...
0
by: alivip | last post by:
Is python provide search in parent folder contain sub folders and files for example folder name is cars and sub file is Toyota,Honda and BMW and Toyota contain file name camry and file name corola,...
11
by: alivip | last post by:
how to ingrate my code to read text in in parent folder contain sub folders and files for example folder name is cars and sub file is Toyota,Honda and BMW and Toyota contain file name Camry and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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,...
0
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...

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.