Search code examples
phpopendir

opendir not reading full file name


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.


Solution

  • 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?