2

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.

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.

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";
1
  • 1
    It seems like what you are trying to do is effectively cache that data. have you looked into some of the established caching solutions, such as memcache, APC etc Commented Nov 3, 2011 at 13:31

3 Answers 3

3

I would also recommend looking at var_export as you won't have to serialize and encode to use it.

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)
?>
Sign up to request clarification or add additional context in comments.

Comments

1

Use PHP's serialize and base64_encode functions and write the result to file.

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

5 Comments

That's a viable solution. The only problem is that it requires unserializing the data every time the file is loaded. I'm not sure if that's slower, faster or the same as parsing a PHP file. But this solution certainly is easier to implement than writing out a valid PHP file.
Honestly it would probably be slower -- why do you feel that simply requiring a php file insufficient?
I'm not saying it's insufficient. But parsing a PHP file is not free either. Do you know which option is faster? I would guess that parsing PHP is faster since the parser is highly optimized, and you may even be able to take advantage of pre-compilation depending on the setup. But I don't actually know if it's faster. In most cases the difference is probably negligible.
I agree, the performance difference would be negligible. Look, if you want to store static content in a seperate page then you will need to include a file in some shape or form. The other option is to store the content that is likely to change in an array at the top of each page -- you won't have to load/parse other files and it would make things easier to maintain (although it's a little messier).
well, i kinda needed to be a readable php file, (as the solution below suggests) rather than encoding the data.
-1

You can directly write the code into the file fwrite($handle, "$array = array();"); then doing an eval() on the file content you're reading back. 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. It's much safer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.