rbvinc Posted January 8, 2010 Share Posted January 8, 2010 I need some help please. I am looking for sample code, or links to do this. I want to display folders, and files in each folder. I want to put 'href" for each file. These are pdf files. Thank you for the help in advance. Folder_A, <a href='a.pdf'>a.pdf</a> <a href='b.pdf'>b.pdf</a> <a href='c.pdf'>c.pdf</a> Folder_B <a href='aa.pdf'>aa.pdf</a> <a href='bb.pdf'>bb.pdf</a> <a href='cc.pdf'>cc.pdf</a> Quote Link to comment Share on other sites More sharing options...
premiso Posted January 9, 2010 Share Posted January 9, 2010 You will want to use glob: foreach (glob("/path/to/folders/", GLOB_ONLYDIR) as $dir) { $folder = explode("/", $dir); $folder = $folder[count($folder)-1]; echo $folder . "<br />"; foreach (glob($dir . "/*.pdf", $file)) { $file = basename($file); echo "<a href='$file'>$file</a><br />"; } } Is one way to do what you want. Quote Link to comment Share on other sites More sharing options...
rbvinc Posted January 10, 2010 Author Share Posted January 10, 2010 Premiso, Thank you for the reply. I am not familiar with php blobs. Help me. I could not follow. I did the following code, and my pdf files are under, C:\Inetpub\wwwroot\newsa\testpdf Error on line #8, foreach (glob($dir . "/*.pdf", $file)) <?php foreach (glob("/newsa/testpdf/", GLOB_ONLYDIR) as $dir) { $folder = explode("/", $dir); $folder = $folder[count($folder)-1]; echo $folder . "<br />"; foreach (glob($dir . "/*.pdf", $file)) { $file = basename($file); echo "<a href='$file'>$file</a><br />"; } } ?> Thank you. Quote Link to comment Share on other sites More sharing options...
rbvinc Posted January 10, 2010 Author Share Posted January 10, 2010 Sorry, typo error. It should read as glob, not blob. Quote Link to comment Share on other sites More sharing options...
premiso Posted January 10, 2010 Share Posted January 10, 2010 My mistake, I put the code in wrong: foreach (glob($dir . "/*.pdf") as $file) Should get you correct results. Quote Link to comment Share on other sites More sharing options...
rbvinc Posted January 10, 2010 Author Share Posted January 10, 2010 Sorry to bother, I ran it, blank screen showing when I ran it, thought I have two pdf files in that folder. C:\Inetpub\wwwroot\newsa\testpdf Error on this new line code, foreach (glob($dir . "/*.pdf") as $file) Please note, I have php version, PHP Version 4.3.11 Thank you, Quote Link to comment Share on other sites More sharing options...
rbvinc Posted January 10, 2010 Author Share Posted January 10, 2010 Full code here, <?php foreach (glob("/newsa/testpdf/", GLOB_ONLYDIR) as $dir) { $folder = explode("/", $dir); $folder = $folder[count($folder)-1]; echo $folder . "<br />"; foreach (glob($dir . "/*.pdf") as $file) { $file = basename($file); echo "<a href='$file'>$file</a><br />"; } } ?> Quote Link to comment Share on other sites More sharing options...
premiso Posted January 10, 2010 Share Posted January 10, 2010 What is the error message being returned? Quote Link to comment Share on other sites More sharing options...
rbvinc Posted January 10, 2010 Author Share Posted January 10, 2010 Just blank screen. Nothing shows up. No error messages trips. Even I refresh the explorer, just comes shows blanks screen. Thank you. Quote Link to comment Share on other sites More sharing options...
premiso Posted January 10, 2010 Share Posted January 10, 2010 Add this to your script: <?php error_reporting(E_ALL); ini_set("display_errors", 1); foreach (glob("/newsa/testpdf/", GLOB_ONLYDIR) as $dir) { $folder = explode("/", $dir); $folder = $folder[count($folder)-1]; echo $folder . "<br />"; foreach (glob($dir . "/*.pdf") as $file) { $file = basename($file); echo "<a href='$file'>$file</a><br />"; } } ?> See what the error says (if any). Quote Link to comment Share on other sites More sharing options...
rbvinc Posted January 10, 2010 Author Share Posted January 10, 2010 My apology for your time, I ran it, none shows up. Just blank white screen, even with couple of refreshes. Quote Link to comment Share on other sites More sharing options...
premiso Posted January 10, 2010 Share Posted January 10, 2010 Chances are the directory is wrong or empty. What is the full path to the directory from the root directory, try using that. Basically what that means is that it never enters the foreach loop, which could be the above, no folders or wrong directory. Quote Link to comment Share on other sites More sharing options...
rbvinc Posted January 10, 2010 Author Share Posted January 10, 2010 I am coping and pasting this from 'my computer' (windows explorer). I have two pdf files under 'testpdf' folder. C:\Inetpub\wwwroot\newsa\testpdf Thank you sir, Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.