如何在脚本中使用PHP文件的内容?

时间:2014-12-15 02:20:40

标签: php ftp file-get-contents

我正在为我的网络应用程序开发管理面板。站点的前端位于与管理面板分开的服务器上。在ACP中有一个表单,我希望能够更改资产的路径(img,js,css等),所以我所要做的就是在表单中更改它们。我创建的辅助函数会将保存的URL应用于任何模板资产。

由于前端和管理面板位于不同的服务器上,我必须使用带有FTP协议的file_get_contents来从管理面板读取前端的资产配置文件。 file_get_contents函数获取资产配置文件的内容,但我希望能够解析文件的实际PHP而不仅仅是显示内容。

示例:

这是config / assets.php

<?php

$config = array(
    'img' => 'http://localhost/frontend/assets/img',
    'css' => 'http://localhost/frontend/assets/css',
    'js' => 'http://localhost/frontend/assets/js',
    'attachments' => 'http://localhost/frontend/attachments'
    );

如何在下面的文件中使用上述文件中的信息....

这是我的MVC(CodeIgniter)控制器:

<?php

class Assets extends MY_Controller {

    function index(){

        $this->load->library('form_validation');
        if($this->form_validation->run('assets') == FALSE){

            $this->template->overall_header("Asset Configuration");

            $this->load->config('assets');

            $config['acp'] = array(
                    'img_url' => $this->config->item('img_url'),
                    'css_url' => $this->config->item('css_url'),
                    'js_url' => $this->config->item('js_url'),
                    'attachment_url' => $this->config->item('attachment_url')
                    );

            $this->load->config('ftp_frontend');
            $content = file_get_contents('ftps://'.$this->config->item('username').':'.$this->config->item('password').'@'.$this->config->item('hostname').'/application/config/assets.php');

            /------------------------------------------------------------------------------/
            /-----  Pass variables to the admin cp form from the content of the front -----/
            /-----  end assets config file ------------------------------------------------/
            /------------------------------------------------------------------------------/

        }

    }

}

我希望我能够很好地解释我的问题!谢谢!

1 个答案:

答案 0 :(得分:0)

如果您启用了allow_url_fopenallow_url_include,则可以使用include来包含远程文件。出于安全原因这样做是不可取的,但为了回答你的问题,你可以做这样的事情。

include_once('ftps://'.$this->config->item('username') .
             ':'.$this->config->item('password') . '@' . 
             $this->config->item('hostname') . 
             '/application/config/assets.php');

var_dump($config);