dqxhit3376 2014-03-20 14: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 15: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

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

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?