将PHP变量保存到文本文件中

时间:2023-01-26 10:07:27

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

我想知道如何将PHP变量保存到txt文件,然后再次检索它们。

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.

有一个输入框,提交后输入框中写入的东西将保存到文本文件中。稍后,结果需要作为变量返回。所以我要说变量是$ text我需要保存到文本文件并能够再次检索它。

6 个解决方案

#1


37  

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

这应该做你想要的,但没有更多的背景,我无法确定。

Writing $text to a file:

将$ text写入文件:

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

#2


34  

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

就个人而言,我使用file_put_contents和file_get_contents(这些是fopen,fputs等的包装器)。

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

#3


4  

(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:

要添加到Christian的答案,您可以考虑使用json_encode和json_decode而不是序列化和反序列化来保证您的安全。请参阅PHP手册页中的警告:

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.

不要将不受信任的用户输入传递给unserialize()。由于对象实例化和自动加载,反序列化可能导致代码被加载和执行,恶意用户可能能够利用它。如果需要将序列化数据传递给用户,请使用安全的标准数据交换格式,如JSON(通过json_decode()和json_encode())。

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

#4


1  

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

for_example,你有anyFile.php,并写有$ any_variable ='hi Frank';

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

要将该变量更改为hi Jack,请使用以下代码:

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

#5


0  

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

使用fopen,fwrite和fread的组合。 PHP.net拥有出色的文档和每个文档的示例。

http://us2.php.net/manual/en/function.fopen.php
http://us2.php.net/manual/en/function.fwrite.php
http://us2.php.net/manual/en/function.fread.php

http://us2.php.net/manual/en/function.fopen.php http://us2.php.net/manual/en/function.fwrite.php http://us2.php.net/manual/ EN / function.fread.php

#6


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)

对变量使用serialize(),然后将字符串保存到文件中。稍后你将能够从文件中读取连续的var并重建原始的var(它是一个字符串,一个数组或一个对象)

#1


37  

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

这应该做你想要的,但没有更多的背景,我无法确定。

Writing $text to a file:

将$ text写入文件:

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

#2


34  

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

就个人而言,我使用file_put_contents和file_get_contents(这些是fopen,fputs等的包装器)。

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

#3


4  

(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:

要添加到Christian的答案,您可以考虑使用json_encode和json_decode而不是序列化和反序列化来保证您的安全。请参阅PHP手册页中的警告:

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.

不要将不受信任的用户输入传递给unserialize()。由于对象实例化和自动加载,反序列化可能导致代码被加载和执行,恶意用户可能能够利用它。如果需要将序列化数据传递给用户,请使用安全的标准数据交换格式,如JSON(通过json_decode()和json_encode())。

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

#4


1  

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

for_example,你有anyFile.php,并写有$ any_variable ='hi Frank';

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

要将该变量更改为hi Jack,请使用以下代码:

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

#5


0  

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

使用fopen,fwrite和fread的组合。 PHP.net拥有出色的文档和每个文档的示例。

http://us2.php.net/manual/en/function.fopen.php
http://us2.php.net/manual/en/function.fwrite.php
http://us2.php.net/manual/en/function.fread.php

http://us2.php.net/manual/en/function.fopen.php http://us2.php.net/manual/en/function.fwrite.php http://us2.php.net/manual/ EN / function.fread.php

#6


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)

对变量使用serialize(),然后将字符串保存到文件中。稍后你将能够从文件中读取连续的var并重建原始的var(它是一个字符串,一个数组或一个对象)