放大器导致返回的文本在URL中被截断

时间:2013-01-28 20:26:15

标签: php urlencode

我之前从维护代码的人那里继承了几个php脚本。他没有任何编程经验,而且大部分都是尽力而为。今天我不得不重写wordpress的插件文件,现在我无法弄清楚出了什么问题。

我面临的问题是,当将返回的文件名放在作为表(<?php echo $result->display_name; ?>)一部分的else语句中时,我会得到意想不到的结果。假设我们的数据库有一个名为Black&amp; amp;的文件。 White.pdf。它不返回display_name“Black&amp; White.pdf”,而是返回“Black”。我尝试过以下方法:

<?php echo htmlentities($result->display_name); ?>

<?php echo htmlspecialchars($result->display_name); ?>

这两者都没有产生预期的结果。出了什么问题?这是我对PHP的无知,还是关于WP如何返回结果的问题(我认为这不会有什么不同,因为我不相信WP会改变php的解析方式)。这里有参考代码:

function display_files($assn_id) {
    global $wpdb;
    $second_db = new wpdb("xxxxxx", "xxxxxx", "xxxxxx", "xxxxxx");

    $results = $second_db->get_results("SELECT
        community_files.id,
        community_files.display_name,
        community_files.filename,
        community_files.sort,
        community_files.file_type
        FROM
        community_files
        WHERE
        community_files.comm_id = '".$assn_id."'
        ORDER BY
        community_files.sort ASC");

        if (!$results) {
            echo "<li>The next meeting has not been posted yet.</li>";
        } else {

            echo "<table>";
            // keeps getting the next row until there are no more to get

            foreach ($results as $result) {
                if (!$result->display_name) {
                    $display_name = str_replace("_", " ", $result->filename);
                    $display_name_fake = str_replace(" .", ".", $display_name);
                    $file_array[$x] = $result->id;
                } else {
                    $display_name = $result->display_name;
                    $display_name_fake = str_replace(" .", ".", $display_name);
                    $file_array[$x] = $result->id;
                } ?>
                    <tr>
                        <td>
                        <?php
                        if ($result->display_name == "") {
                        ?>
                            <a href="renamefiles.php?action=rename&amp;file_id=<?php echo $result->id; ?>&amp;filename=<?php echo $display_name; ?>&amp;assn_id=<?php echo $assn_id; ?>"><?php echo $display_name_fake; ?></a>
                        <?php
                        } else {
                        ?>
                            <a href="renamefiles.php?action=rename&amp;file_id=<?php echo $result->id; ?>&amp;filename=<?php echo $display_name; ?>&amp;assn_id=<?php echo $assn_id; ?>"><?php echo $result->display_name; ?></a>
                        <?php
                        }
                        ?>
                        </td>
                    </tr>
                <?php   
            }
            echo "</table>";
        }
}

1 个答案:

答案 0 :(得分:1)

当您将文件名插入正在构建的URL时,听起来需要urlencode该文件名。根据上述评论,不执行此操作会破坏您的GET字符串。

array(5) { 
  ["action"]=> string(6) "rename" 
  ["file_id"]=> string(5) "24086" 
  ["filename"]=> string(8) "Black " 
  ["White_pdf"]=> string(0) "" 
  ["assn_id"]=> string(2) "25" 
}

这使您得出结论,您发布的代码失败了。事实并非如此。它只是构建格式错误的HTML,然后单击它时会失败。

此代码:

<a href="renamefiles.php?action=rename&amp;file_id=<?php echo $result->id; ?>&amp;filename=<?php echo $display_name; ?>&amp;assn_id=<?php echo $assn_id; ?>"><?php echo $result->display_name; ?></a>

应该是这样的:

<a href="renamefiles.php?action=rename&amp;file_id=<?php echo $result->id; ?>&amp;filename=<?php echo urlencode($display_name;) ?>&amp;assn_id=<?php echo $assn_id; ?>"><?php echo $result->display_name; ?></a>

处理请求时,您需要urldecode $_GET['filename']

我建议您不要在文件名中包含&之类的空格和特殊字符(如果您实际使用此名称作为文件系统文件名)。你在找麻烦。

相关问题