jculkincys
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
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
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($file ext)-1) . "</a>";
}
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'>"
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Beauty ldbkutty
just one extra ")" in if(strtolower($fileext) == "pdf")){
just one extra ")" in if(strtolower($fileext) == "pdf")){
:=)
Guided by the ee newsletter here. What would be the asp alternative?
Regards,
Fahd Murtaza
Regards,
Fahd Murtaza
Hmm...I suggest to post in the ASP section.
<?php
if ($handle = opendir('/path/')) {
while (false !== ($file = readdir($handle))) {
$file = pathinfo('file');
$ext = strtolower($file_parts['ex tension']) ;
if($ext == "pdf")
echo "$file\n";
}
closedir($handle);
}
?>
if ($handle = opendir('/path/')) {
while (false !== ($file = readdir($handle))) {
$file = pathinfo('file');
$ext = strtolower($file_parts['ex
if($ext == "pdf")
echo "$file\n";
}
closedir($handle);
}
?>
See the user comments for some example code.