1

I want to open a directory in php and then display the link for downloading that file.

this is what I am trying.

if(is_dir($dir))
{
  $op=opendir($dir);
  echo"Files in the directiry are<br>";

   while(($file=readdir($op))!==false)
   {
    if(strpos($file,$ext,1))
   {
      echo "<a href=apps/".$file .">".$file."</a><br>";
    }  
  }
}

this shows downloading links but only upto space.

Kapil
  • 817
  • 2
  • 13
  • 25

1 Answers1

1

PHP function rawurlencode apparently gives you better system coverage. urlencode actually doesn't work on my localhost.

<?php

$dir = 'apps';
$ext = 'pdf';

if(is_dir($dir))
{
  $op=opendir($dir);
  echo"Files in the directory are<br>";

   while(($file=readdir($op))!==false)
   {
    if(strpos($file,$ext,1))
   {
      echo '<a href="apps/' . rawurlencode($file) . '">' . $file . '</a><br>';
    }  
  }
}

?>

More on the subject here: urlencode vs rawurlencode?

Community
  • 1
  • 1
bloodyKnuckles
  • 11,551
  • 3
  • 29
  • 37