38

I was wondering how to save PHP variables to a txt file and then retrieve them again.

Example:

There is an input box, after submitted the stuff that was written in the input box will be saved to a text file. Later on the results need to be brought back as a variable. So lets say the variable is $text I need that to be saved to a text file and be able to retrieve it back again.

5
  • actually you cannot save a variable but only it's value. Commented Jun 8, 2010 at 7:39
  • Actually, you can save variables, but not in a clean way. It's always possible to compact() your variables in an array and later extract() them, though.
    – Duroth
    Commented Jun 8, 2010 at 7:45
  • @Duroth you are absolutely right.If you can please give me more idea.Thank you
    – Aadi
    Commented Jun 8, 2010 at 7:50
  • 2
    (sidenote) If you only want to persist the form data between requests, consider using Sessions.
    – Gordon
    Commented Jun 8, 2010 at 8:15
  • All the answers are answering your specific question, but Gordon is absolutely right: It really sounds like you want Sessions. Commented Jun 8, 2010 at 18:51

7 Answers 7

68

This should do what you want, but without more context I can't tell for sure.

Writing $text to a file:

$text = "Anything";
$var_str = var_export($text, true);
$var = "<?php\n\n\$text = $var_str;\n\n?>";
file_put_contents('filename.php', $var);

Retrieving it again:

include 'filename.php';
echo $text;
4
  • thank you very much.You are a genius.Actually this is what I am looking for...!!Working perfectly.Thanks again dear friend
    – Aadi
    Commented Jun 8, 2010 at 11:06
  • 2
    Bad way, because problem with security. You can included some code with vulnerability.
    – Michail M.
    Commented Oct 26, 2016 at 13:13
  • @mishanon Who has access to this file except you? Commented Aug 24, 2018 at 20:49
  • 2
    Note that returning value is possible too: <?php return $var_str. Then you can use a different name for included data e.g. $someText = (include 'filename.php');
    – Nux
    Commented Jan 17, 2019 at 13:59
52

Personally, I'd use file_put_contents and file_get_contents (these are wrappers for fopen, fputs, etc).

Also, if you are going to write any structured data, such as arrays, I suggest you serialize and unserialize the files contents.

$file = '/tmp/file';
$content = serialize($my_variable);
file_put_contents($file, $content);
$content = unserialize(file_get_contents($file));
1
7

(Sorry I can't comment just yet, otherwise I would)

To add to Christian's answer you might consider using json_encode and json_decode instead of serialize and unserialize to keep you safe. See a warning from the PHP man page:

Warning

Do not pass untrusted user input to unserialize(). Unserialization can result in code being loaded and executed due to object instantiation and autoloading, and a malicious user may be able to exploit this. Use a safe, standard data interchange format such as JSON (via json_decode() and json_encode()) if you need to pass serialized data to the user.

So your final solution might have the following:

$file = '/tmp/file';
$content = json_encode($my_variable);
file_put_contents($file, $content);
$content = json_decode(file_get_contents($file), TRUE);
3

for_example, you have anyFile.php, and there is written $any_variable='hi Frank';

to change that variable to hi Jack, use like the following code:

<?php
$content = file_get_contents('anyFile.php'); 

$new_content = preg_replace('/\$any_variable=\"(.*?)\";/', '$any_variable="hi Jack";', $content);

file_put_contents('anyFile.php', $new_content);
?>
1
  • this is what i need after saving, i need updating. Thanks @T.Todua
    – bdalina
    Commented Sep 9, 2017 at 8:32
0

Use a combination of of fopen, fwrite and fread. PHP.net has excellent documentation and examples of each of them.

https://www.php.net/manual/en/function.fopen.php
https://www.php.net/manual/en/function.fwrite.php
https://www.php.net/manual/en/function.fread.php

5
  • Thank you for your caring.Unfortunately, I think you don't get me.
    – Aadi
    Commented Jun 8, 2010 at 7:37
  • 1
    There is no problem with the references.Which is very helpful.Thank you once again.But, this is not actually what I am looking for.
    – Aadi
    Commented Jun 8, 2010 at 7:47
  • You asked for a way to write contents to a find (fwrite) and a way to read it back into a variable (fread) I personally agree that Christian's answer is better, but this does the same thing. Commented Jun 8, 2010 at 7:48
  • Ajith, if my answer solved your question, then this one would have also as 'file_put_contents' is simply a wrapper function for the functions in Kerry's answer. Perhaps we both misunderstood though?
    – Christian
    Commented Jun 8, 2010 at 7:50
  • 3
    You have to put more effort in describing what you want then if none of the suggestions here help. Commented Jun 8, 2010 at 7:51
0

Use serialize() on the variable, then save the string to a file. later you will be able to read the serialed var from the file and rebuilt the original var (wether it was a string or an array or an object)

0

Okay, so I needed a solution to this, and I borrowed heavily from the answers to this question and made a library: https://github.com/rahuldottech/varDx (Licensed under the MIT license).

It uses serialize() and unserialize() and writes data to a file. It can read and write multiple objects/variables/whatever to and from the same file.

Usage:

<?php
require 'varDx.php';
$dx = new \varDx\cDX; //create an object
$dx->def('file.dat'); //define data file
$val1 = "this is a string";
$dx->write('data1', $val1); //writes key to file
echo $dx->read('data1'); //returns key value from file

See the github page for more information. It has functions to read, write, check, modify and delete data.

Not the answer you're looking for? Browse other questions tagged or ask your own question.