
learner007
-
Posts
5 -
Joined
-
Last visited
Posts posted by learner007
-
-
Oh, right, that was it.
I hope I am not confusing you? I dont want the results to have all files in one list and all directories in separate list. I dont mind the way code is listing the files and directories, but it is duplicating the results, if you look at my previous post you can see the files and directories of same name listing several times. How to fix that?
-
Someone (I think it was me) originally suggested the RecursiveDirectoryIterator in another thread, but the output requirements made it easier to use scandir/glob and do the recursion manually.
...at least that's what I remember. I thought it was supposed to show files before directories too, but the posted code won't do that.
Mr requinix, I've posted this on devshed, I've taken your suggestions and got the codes to work this far, now stuck to this point. I've never done nested foreach loop before, I cant get foreach loop to behave correctly, listing same file and directory more than once? Also the root directory name should show on top and files inside it should show just below it but instead just get listed anywhere in the list of files.
After correcting the file_match($full_path, $patterns)
Also I've renamed few files so that I can follow what directory they belong to.
The output result I got:
February Files
February File 1.pdf
February File 2.pdf
February Files
February File 1.pdf
February File 2.pdf
February Files
February File 1.pdf
February File 2.pdf
File List.pdf
January Files
January File 1.pdf
January File 2.pdf
January Files
January File 1.pdf
January File 2.pdf
January Files
January File 1.pdf
January File 2.pdf
Read First.pdf
Zip File.zip
word.docx
Output should be like this:Monthly FilesRead First.pdfZip File.zip
word.docx
January FilesJanuary File 1.pdf
January File 2.pdf
February FilesFebruary File 1.pdf
February File 2.pdf
-
I've tried with DirectoryIterator but I couldnt get it to work and the output result was not as expected. Would you be able to give examples of codes?
-
I been banging my head over this for sometimes now, But I cant get the result I am after, I was hoping someone can liberate me from the forever circle of mistakes..
function file_match($file_path, array $patterns) { $files = array_diff(scandir($file_path), array('.', '..')); foreach($files as $file) { $full_path = $file_path . DIRECTORY_SEPARATOR . $file; foreach($patterns as $pattern) { if (is_file($full_path)) { if (fnmatch(strtolower($pattern), $file)) { echo $file; echo "<br>"; } } elseif (is_dir($full_path)) { echo $file; echo "<br>"; file_match($full_path, $pattern); } } } } $file_path = 'X:/testfolder1/Monthly Files'; echo file_match($file_path, ['*.doc', '*.zip', '*.pdf]);
Expecting result:
Directory1
List of files resides inside this directory
Directory 2
List of files Resides inside this directory
etc
like:
This is what it should be:
Monthly Files
Test File Main 2.PDF
Test File Main 1.doc
January Files
File January 1.PDF
File January 2.zip
February Files
Files February 1.PDF
Files February 2.PDFetc
Recursive function and too many foreach loop tangled
in PHP Coding Help
Posted · Edited by learner007
This will work, with basename this will list the directories and files.
output:
Monthly Files <------------root folder
February Files <----------------sub folder
February File 1.pdf
February File 2.pdf
File List.pdf <--------------root files
January Files <------------- sib folder
January File 1.pdf
January File 2.pdf
Read First.pdf <----------- root file
Zip File.zip <--------------- root file
How can I achieve this: List any root files in the root folder listing first, Then any sub folder and its contents listing?
Monthly Files <------------root folder
Read First.pdf <----------- root file
Zip File.zip <--------------- root file
File List.pdf <--------------root file
February Files <----------------sub folder
February File 1.pdf
February File 2.pdf
January Files <---------sub folder
January File 1.pdf
January File 2.pdf