开发者

Read and Write to php file

开发者 https://www.devze.com 2023-01-27 17:45 出处:网络
I am looking for a method to write the values of some variable to a PHP file. Ok let me explain by example:

I am looking for a method to write the values of some variable to a PHP file.

Ok let me explain by example:

I have a normal PHP website and want to add WordPress as a blog to it.

I use the same theme as of my WordPress for the other site too, removing the WordPress tags and replacing them with my other application’s tags. So here the Div IDs and Class names remain the same. Also I share the same CSS & JS files as WordPress.

I want to control the width of my sidebars and some other parameters from the WordPress Theme control panel. The process I think should be…

get value for 'sidebar_a' and say inputs is '150px' get value for 'main_body' and say inputs is '550px' get value for 'sidebar_b' and say inputs is '200px' get value for ‘site_name’ and say the input comes from the WordPress tag get value for ‘footer ‘and say again the value comes from the 开发者_开发知识库WordPress footer.

Now I need a small script which will write these values in a PHP file like:

$sidebar_a = '100'

$main_body = '550'

$sidebar_b = '200'

$sitename = ‘XYZ Corporation’

$footer = ‘Copyright XYZ corporation'

or better create some database tables in MySQL and write to it.

I would then want to call this file in my application 2 using

Please feel free to suggest, if there is a better way.

IMPORTANT: Every time these parameters are changed in WordPress, these values must be updated with the new values.

Kindly help.


Write data

$data = array(
    'sidebar_a' => $sidebar_a,
    'main_body' => $main_body,
    'sidebar_b' => $sidebar_b,
    'sitename' => $sitename,
    'footer' => $footer
);
$data = serialize($data);
file_put_contents('file.txt', $data);

Read data

$data = file_get_contents('file.txt');
$data = unserialize($data);
extract($data);

See serialize and extract

http://php.net/serialize

http://php.net/extract


To store PHP variables in a file (as the topic states) I would store all the configuration values into an array and then use the serialize()/unserialize() functions and then read/write to a file.

Eg:

file_put_contents('myconf.txt', serialize($my_array));
$my_array = unserialize(file_get_contents('myconf.txt'));

If you absolutely need to include the created configuration file somewhere else in order to get the configuration (I don't see why, but you possibly could do it), you can instead use something like this:

file_put_contents('myconf.php', '$my_array = '. var_export($my_array, TRUE) .';');

and then just

include 'myconf.php';

..then I think you should create some patch to Wordpress core to make it write to this file every time the configuration values are changed.

Or, a better solution IMHO, would be to make the other application read directly from the Wordpress database, in order to avoid possible incoherences and all the problems derived by patching some third-party application code.

0

精彩评论

暂无评论...
验证码 换一张
取 消