Jump to content

learner007

New Members
  • Posts

    5
  • Joined

  • Last visited

Everything posted by learner007

  1. This will work, with basename this will list the directories and files. const PATH = 'X:/testfolder1/Monthly Files'; const EXTENSIONS = ['doc', 'zip', 'pdf']; $dirIterator = new RecursiveDirectoryIterator(PATH, RecursiveDirectoryIterator::SKIP_DOTS); $fileIterator = new RecursiveIteratorIterator($dirIterator, RecursiveIteratorIterator::SELF_FIRST); echo basename(PATH); echo "<br><br>"; foreach ($fileIterator as $file) { if ($file->isDir() || ($file->isFile() && in_array(strtolower($file->getExtension()), EXTENSIONS))) { echo basename($file)."<br>"; } } 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
  2. 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?
  3. 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 Files Read First.pdfZip File.zip word.docx January FilesJanuary File 1.pdf January File 2.pdf February FilesFebruary File 1.pdf February File 2.pdf
  4. 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?
  5. 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.PDF etc
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.