Link to home
Start Free TrialLog in
Avatar of jculkincys
jculkincysFlag for United States of America

asked on

Looping through files in a folder

Hello

Would someone tell me an easy way to loop through files in a folder. Ideally I would like to only see files of one kind (.pdf) so that I could place a link to them on a web page.

I would like to dynamically create the following HTML

FILES IN FOLDER "TEST"
<a href=test/1.pdf" 1 </a>
<a href=test/2.pdf" 2 </a>
<a href=test/3.pdf" 3 </a>

Thanks
Avatar of snoyes_jw
snoyes_jw
Flag of United States of America image

http://www.php.net/dir

See the user comments for some example code.
http://uk.php.net/manual/en/function.readdir.php

Assuming you are able to read all files in the loop with $file :

$fileext = substr($strrchr($file, "."), 1);
if(strtolower($fileext) == "pdf")){
  echo "<a href='folder_name/$file'>" . substr($file, 0, strlen($file)-strlen($fileext)-1) . "</a>";
}
ASKER CERTIFIED SOLUTION
Avatar of ldbkutty
ldbkutty
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jculkincys

ASKER

Beauty ldbkutty

just one extra ")" in   if(strtolower($fileext) == "pdf")){
Guided by the ee newsletter here. What would be the asp alternative?
Regards,
Fahd Murtaza
Hmm...I suggest to post in the ASP section.
Avatar of antido
antido

<?php

if ($handle = opendir('/path/')) {

   while (false !== ($file = readdir($handle))) {
       
       $file = pathinfo('file');
       $ext = strtolower($file_parts['extension']);

       if($ext == "pdf")
              echo "$file\n";

   }

   closedir($handle);
}
?>