dse84825 2013-01-04 15:05
浏览 73
已采纳

php自动创建基于带空格的文件名的超链接

Probably just something small, but I am using the below code to read the files inside of a directory and print them out as of a hyperlink to itself.

It prints out the complete file name, but it creates an incomplete hyperlink using only characters up to the first space. The question is how to create a functional hyperlink for a file with spaces in it?

$dir = '.';
$dh = opendir($dir);
$file_count = 0;
while (false !== ($file = readdir($dh))) {
    $files[] = $file;
    $file_count += 1;
    echo $file;
}
for ($x = 0; $x < $file_count; $x += 1) {
    echo "<a href=$files[$x]>" . "$files[$x]" . "</a><br>";
}
  • 写回答

4条回答 默认 最新

  • dougu6815 2013-01-04 15:11
    关注

    You may wish to add quotes to the HREF attribute inside the anchor tag:

     echo "<a href='$files[$x]'>$files[$x]</a><br>";
    

    The client may think the part of the filename following the space is another attribute. (You may also want to use htmlentities() to escape the filenames inside the href attribute.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部