dqxhit3376 2014-03-20 06:36
浏览 28
已采纳

当文件名中出现空格时,PHP Echo停止

I'm trying to show an pdf file stored local in the browser with this code:

<p><a href=<?php
$sql = "select title from tasks where taskPK='".$_SESSION['task']."'";
$res = $db->query($sql);
if (!$res) {
    throw new Exception("Database Error [{$db->errno}] {$db->error}");
}
while($row = $res->fetch_assoc()){
    $rad= $row['title'];
echo ("files/".$id."/".$rad.".pdf"); }?> > See PDF </a> </p>

This should go into the right folder and get the file and then show it. But my problem is that if the name of the file has an space for example "peter pan.pdf" Then it won't work. The URL is supposed to be localhost/upload/files/9/Peter%20Pan.pdf But that doesn't happen. The URL is localhost/upload/filer/9/Peter It stops when the space hits and the file doesn't show.

But if the filename is connected with a - like this: "Peter-Pan.pdf" it works. Then the URL becomes localhost/upload/filer/9/Peter-Pan.pdf And the PDF shows.

Whats the problem? Sorry if it is a bit messy.

$id is the user wich becomes 9 in the URL

The problem should be in here but i can't figuire it out:

echo ("files/".$id."/".$rad.".pdf"); }?> > See PDF </a> </p>
  • 写回答

1条回答 默认 最新

  • doukong1897 2014-03-20 07:14
    关注

    White space is an illegal URL character. Use urlencode to clean $rad.

     $rad = urlencode($row['title']);
    

    Regards,

    EDIT:

    $rad = rawurlencode($row['title']);
    

    This is the reference to rawurlencode

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

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

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

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

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

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

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

客服 返回
顶部