简体   繁体   中英

How can I use the contents of a PHP file in my script?

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!

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM