douzi9430 2014-12-15 02:20
浏览 29
已采纳

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

I am developing the admin panel for my web application. The front end of the site is on a server separate from the admin panel. There is a form in the ACP where I want to be able to change the paths of my assets(img, js, css, etc) so all I have to do is change them in the form. A helper function I created will apply the saved URL to any template assets.

Since the frontend and admin panel are on different servers, I have to use file_get_contents with the FTP protocol to read the assets config file of the frontend from the admin panel. The file_get_contents function gets the content of the assets config file, but I want to be able to parse the actual PHP of the file and not just display the contents.

Example:

This is 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'
    );

How can I use the information from the file above in the file below....

This is my MVC(CodeIgniter) controller:

<?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 ------------------------------------------------/
            /------------------------------------------------------------------------------/

        }

    }

}

I hope I am explaining my problem well enough! Thanks!

  • 写回答

1条回答 默认 最新

  • dongyong1897 2014-12-15 02:31
    关注

    If you have allow_url_fopen and allow_url_include enabled, you can simply use include to include remote file. It is not advisable to do so for security reason, but for the sake of answer your question, you can do something like this.

    include_once('ftps://'.$this->config->item('username') .
                 ':'.$this->config->item('password') . '@' . 
                 $this->config->item('hostname') . 
                 '/application/config/assets.php');
    
    var_dump($config);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改