Xml 如何使用ZendFramework 1输出文本文件?

Xml 如何使用ZendFramework 1输出文本文件?,xml,zend-framework,Xml,Zend Framework,我正在编写一个ZendFramework(1.4)应用程序,需要在其中显示特定文件的内容。这是我的代码: public function showfileAction() { $this->_helper->layout->disableLayout(); $configOptions = $this->getInvokeArg('bootstrap')->getOptions(); $file = $configO

我正在编写一个ZendFramework(1.4)应用程序,需要在其中显示特定文件的内容。这是我的代码:

public function showfileAction()
{
        $this->_helper->layout->disableLayout();
        $configOptions = $this->getInvokeArg('bootstrap')->getOptions();

        $file = $configOptions['files']['dir'] . $this->getRequest()->getParam('file');
        if ( is_readable( $file ) ) {
            foreach ( $this->getRequest()->getParam('headers') as $h => $v ) {
                $this->getResponse()->setHeader( $h, $v );
            }
            $this->view->file_contents = file_get_contents($file);
        } else {
            $this->_redirect ('/not-found');
        }
}
我试图显示的特定文件是一个xml(我尝试了几种不同的内容类型,结果相同),我得到的是由制表符(!)前缀的文件内容,在xml的情况下,这会导致如下错误(取自google chrome):

我的文件的内容:

<?xml version="1.0"?>
<users>
    <user>XXXX</user>
</users>

没有必要为此使用视图脚本,因此我可能会更改您必须更改的内容:

public function showfileAction()
{
    $this->getHelper('viewRenderer')->setNoRender(true);
    $this->getHelper('layout')->disableLayout();

    $configOptions = $this->getInvokeArg('bootstrap')->getOptions();

    $file = $configOptions['files']['dir'] . $this->getRequest()->getParam('file');
    if ( is_readable( $file ) ) {
        foreach ( $this->getRequest()->getParam('headers') as $h => $v ) {
            $this->getResponse()->setHeader( $h, $v );
        }
        $this->getResponse()->setBody(file_get_contents($file));
    } else {
        $this->_redirect ('/not-found');
    }
}
如果您正在呈现的XML文件中没有该标签,则可能是由于关闭应用程序中的某个PHP文件后出现空格而导致的。要找出哪一种方法是最好的,并不是一个简单的方法——最好的做法是从纯PHP文件中省略最后一个
?>
,以避免这个问题


关于安全性的另一个警告-即使您认为您的路由限制已经足够了,如果您的应用程序仍然使用默认的
:controller/:action
路由,那么用户可能可以绕过该路由,因此请小心您将哪些内容引入该控制器

允许用户输入设置文件路径和响应头似乎有点冒险…@timfuntain-yes。。。我明白你的意思,我会看一看,但我认为我很好,因为这个动作被一个非常具体的路由规则捕获。谢谢你的回答。我将尝试计算额外的制表符,可能就是这样。关于安全性,我特别尝试了url/showfile/xx.txt,并验证了它不可访问(我不使用默认路由)。
    <?xml version="1.0"?>
<users>
    <user>XXXX</user>
</users>
<?php
echo $this->file_contents;
?>
public function showfileAction()
{
    $this->getHelper('viewRenderer')->setNoRender(true);
    $this->getHelper('layout')->disableLayout();

    $configOptions = $this->getInvokeArg('bootstrap')->getOptions();

    $file = $configOptions['files']['dir'] . $this->getRequest()->getParam('file');
    if ( is_readable( $file ) ) {
        foreach ( $this->getRequest()->getParam('headers') as $h => $v ) {
            $this->getResponse()->setHeader( $h, $v );
        }
        $this->getResponse()->setBody(file_get_contents($file));
    } else {
        $this->_redirect ('/not-found');
    }
}