简体   繁体   English

创建一个动态的php文件并定义一个数组

[英]create a dynamic php file and define an array

I need to update some static site data periodically, so i am thinking of keeping it in a format easily and readily available on the server. 我需要定期更新一些静态站点数据,所以我想在服务器上轻松地保存它的格式。

I am thinking of creating a php file that can be included in the code, so preparing the data gets separate from the browser requests. 我正在考虑创建一个可以包含在代码中的php文件,因此准备数据会与浏览器请求分开。

i fetch the data from the db, so i have an array right now in key value format. 我从数据库获取数据,所以我现在有一个键值格式的数组。

now i want to create a php file, that just defines that array. 现在我想创建一个PHP文件,该文件仅定义了该数组。

the key and value will be string based values, and the data is in swedish. 键和值将是基于字符串的值,数据是瑞典语。

is there a way to directly dump the array to a file, and then just include the file without any preprocessing. 有没有办法直接将数组转储到文件,然后只需包含该文件,无需任何预处理。 I want the file in the following output : 我想在以下输出中的文件:

$array = array ();
$array ["key"] = "value";
$array ["key"] = "value";
$array ["key"] = "value";
$array ["key"] = "value";

I would also recommend looking at var_export as you won't have to serialize and encode to use it. 我还建议您查看var_export,因为您无需序列化和编码即可使用它。

For example (untested): 例如(未经测试):

<?php
$array = array('key' => 'v');
$exported = var_export($array, TRUE);
file_put_contents('/path/to/file', '<?php $var = ' . $exported . '; ?>');
?>

and then you can read it back in via: 然后你可以通过以下方式阅读:

<?php
include '/path/to/file';
// and now you have access to $var
// of course you may want to change the name of the $var variable as it
// will be brought into global scope (and might conflict)
?>

Use PHP's serialize and base64_encode functions and write the result to file. 使用PHP的serializebase64_encode函数并将结果写入文件。

When you want to use the data: simply read the file, decode and unserialize: 当您想要使用数据时:只需读取文件,解码并反序列化:

 <?php // dump data $array = array("key"=>"value"); file_put_contents("test_data.txt", base64_encode(serialize($array))); 
  // retrieve data
  $string = file_get_contents("test_data.txt");
  $data = unserialize(base64_decode($string)));
  var_dump($data);

Encoding the array in base64 will allow you to safely write binary data to the file (eg. extended character sets). 在base64中对数组进行编码将允许您安全地将二进制数据写入文件(例如,扩展字符集)。

You can directly write the code into the file fwrite($handle, "$array = array();"); 您可以直接将代码写入文件fwrite($ handle,“$ array = array();”); then doing an eval() on the file content you're reading back. 然后对要读取的文件内容执行eval()。 It'll work, but its dangerous because if any malicious code is put into the text file, it will be executed. 它可以工作,但是很危险,因为如果将任何恶意代码放入文本文件,它将被执行。

I recommend you take a look at serialization: http://php.net/manual/en/function.serialize.php and write serialized data that you unserialize back into an array. 我建议您看一下序列化: http : //php.net/manual/en/function.serialize.php并将未序列化的序列化数据写回到数组中。 It's much safer. 它更安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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