简体   繁体   English

带空格的文件名未在 html 链接中打开

[英]filename with space are not opening in html links

function to display list of files. function 显示文件列表。 this code is to display the list of files in a folder/directory in a tabular format.此代码以表格格式显示文件夹/目录中的文件列表。 list of displayed files are to be clickable.显示的文件列表是可点击的。 output is coming but file name having any gap in between are not opening on click. output 即将推出,但文件名之间有任何间隙在点击时无法打开。 instead i tried this line in above code instead but still not opening and display of file name also looks odd as '%20' is added wherever space found in the filename.相反,我在上面的代码中尝试了这一行,但仍然没有打开,并且文件名的显示也看起来很奇怪,因为在文件名中找到空间的任何地方都添加了“%20”。 please suggest.请建议。

function outputFiles($path) 
    {
    if(file_exists($path) && is_dir($path))
    {
        $result = scandir($path);
        $files = array_diff($result, array('.', '..'));

                  // output file list in HTML TABLE format
                  echo "<table align=center border=\"1\">\n";
                  echo "<thead>\n";
                  echo "<tr><th>Filename</th></tr>\n";
                  echo "</thead>\n";
                  echo "<tbody>\n";
                  foreach($files as $file) 
                  {
                    echo "<tr>\n";
                    echo "<td><a href=$path/$file>".basename($file)."</a></td>";
        // echo "<td><a href=$path/$file>".rawurlencode($file)."</a></td>";  tried this also
                    echo "</tr>\n";
                  }
                  echo "</tbody>\n";
                  echo "</table>\n\n";
    } else 
    {
        echo "ERROR: The directory does not exist.";
    }
}
?>

You need to put the href value inside quotations.您需要将href值放在引号内。 It is always good practice to put any tag attribute in quotations.将任何标记属性放在引号中始终是一种好习惯。 So just change所以只要改变

<a href=$path/$file>

to

<a href='$path/$file'>

or或者

<a href=\"$path/$file\">

The problem is when you have space in filename, lets say the file name is foo bar and you write问题是当您在文件名中有空格时,假设文件名是foo bar并且您写

<a href=$path/$file>

it'll become它会变成

<a href=foo bar>

which actually means the value of href attribute is foo and we have another attribute called bar , but when you write这实际上意味着href属性的值是foo并且我们还有另一个名为bar的属性,但是当你写

<a href='$path/$file'>

it'll become它会变成

<a href='foo bar'>

which means the value of href attribute is foo bar .这意味着href属性的值为foo bar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM