opendir没有读取完整的文件名

时间:2014-05-27 14:08:23

标签: php opendir

我想在php中打开一个目录,然后显示下载该文件的链接。

这就是我正在尝试的。

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>";
    }  
  }
}

这显示下载链接但仅限于空间。

1 个答案:

答案 0 :(得分:1)

PHP函数rawurlencode显然可以为您提供更好的系统覆盖率。 urlencode实际上并不适用于我的本地主机。

<?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>';
    }  
  }
}

?>

有关此主题的更多信息,请访问:urlencode vs rawurlencode?

相关问题