将 PHP 变量保存到文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2995461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:21:42  来源:igfitidea点击:

Save PHP variables to a text file

phpfile

提问by Aadi

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 我需要将其保存到文本文件中并能够再次检索它。

回答by Sandeep Shetty

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;

回答by Christian

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

回答by duckboy81

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

(对不起,我现在还不能发表评论,否则我会)

To add to Christian's answer you might consider using json_encodeand json_decodeinstead of serializeand unserializeto keep you safe. See a warning from the PHP man page:

为了增加 Christian 的答案,您可能会考虑使用json_encodeandjson_decode代替serializeandunserialize来保证您的安全。请参阅 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);

回答by T.Todua

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 Hyman, use like the following code:

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

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

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

file_put_contents('anyFile.php', $new_content);
?>

回答by MarcoSpada

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(无论它是字符串、数组还是对象)

回答by Kerry Jones

Use a combination of of fopen, fwriteand fread. PHP.net has excellent documentation and examplesof 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/ zh/function.fread.php

回答by undo

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

好的,所以我需要一个解决方案,我从这个问题的答案中大量借鉴并制作了一个库:https: //github.com/rahuldottech/varDx(在 MIT 许可下获得许可)。

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.

它使用serialize()unserialize()将数据写入文件。它可以从同一个文件中读取和写入多个对象/变量/任何内容。

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.

有关更多信息,请参阅 github 页面。它具有读取、写入、检查、修改和删除数据的功能。