在 PHP 中编辑文件

发布于 2024-08-21 21:08:30 字数 117 浏览 9 评论 0原文

有没有办法使用 PHP 从文件中追加或删除一行文本。

我正在为我的特定网络托管堆栈编写托管控制面板,并且希望能够以最少的接触文件系统的要求对文件进行更改,因此希望不必重写整个文件文件来添加或删除配置选项。

Is there a way to append or remove a line of text from a file using PHP.

I am in the process of writing a hosting control panel for my specific web hosting stack and would like to be able to make changes to the files with minimal requirements to touch the file system, and as such would like not to have to rewrite the whole file to add or remove a configuration option.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

野生奥特曼 2024-08-28 21:08:30

如果不首先将文件解析为行然后再次将其写出,则无法从文件中删除行。

您可以使用 fopen$mode 附加到文件 设置为“a”

$fp = fopen('myfile', 'a');

There is no way to remove a line from a file without first parsing the file into lines and then writing it out again.

You can append to a file by using fopen with the $mode set to 'a'

$fp = fopen('myfile', 'a');
那小子欠揍 2024-08-28 21:08:30

对于追加,您应该使用 fopena$mode

请查看此内容 ,关于如何从文件中删除一行。

For appending, you should use fopen with the $mode of a.

See this please, on how to delete a line from the file.

舂唻埖巳落 2024-08-28 21:08:30

是的,您可以以附加模式打开文件:

$fh = fopen('testFile.txt', 'a');

如果您现在写入文件,则会附加新内容。

请参阅 fopen 并从此文档中:

'a':仅开放用于写入;将文件指针放在文件末尾。如果该文件不存在,请尝试创建它。

但删除最后一行是不可能的。

Yes, you can open files in append mode:

$fh = fopen('testFile.txt', 'a');

If you now write to the file, the new content gets appended.

See fopen and from this documentation:

'a': Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

Removing the last line is not possible though.

慵挽 2024-08-28 21:08:30

在 PHP 中处理文件充满了陷阱,尤其是并发性和锁定方面。您编写配置文件的事实还意味着您正在公开通常仅对网络上具有 root 权限的用户可用的任务。您在帖子中没有提到这些事情,但与解决这些问题相比,您的问题是微不足道的。

关于你的问题 - 尽管实现你的建议相当简单(例如通过 exec'ing sed - 尽管你没有说它运行在什么操作系统上),但我建议以其他表示形式创建原始文件的副本 -明显的候选者是数据库 - 您可以在其中将序列号应用于行并轻松创建要填充的间隙,或者存储在会话中的 PHP SplDoublyLinkedList。然后,一旦用户完成了所需数量的更改,就可以通过工作表示形式通过单个操作重新生成文件。

请注意,最终,无论您如何实现该解决方案,该解决方案都将重写整个文件 - 这只是一个问题,即该过程中有多少内容在您的代码中公开。

请记住,您所做的与大多​​数 php Web 脚本相同 - 除了当它们操作和 reqrite HTML 时,您正在使用不同的文件类型 - 因此您可能想看看其他 PHP 模板系统是如何做的已编写,并考虑是否可以为配置文件创建模板。

HTH

C.

Working with files in PHP is full of pitfalls, particularly wrt concurrency and locking. The fact that you are writing config files implies also that you are exposing tasks normally only available to a user with root privileges over the web. You did not mention these things in your post, but your question is trivial in comparison to addressing these issues.

Regarding your question - although its fairly trivial to implement what you propose (e.g. by exec'ing sed - although you did not did say what OS this is running on) I'd recommend creating a copy of the original file in some other representation - obvious candidates would be a database - where you can apply sequence numbers to the lines and easily create a gap to populate, or a PHP SplDoublyLinkedList stored in the session. Then once the user has effected as many changes as they require, regenerate the file in a single operation from the working representation.

Note that, ultimately, regardless of how you implement the solution the solution will rewrite the entire file - its just a question of how much of this process is exposed within your code.

Bear in mind, that what you are doing is just the same as most php web scripts - except while they manipulate and reqrite HTML, you're doing it with a different file type - so you might want to look at how other PHP templating systems are written, and consider whether you can create a template for your config files.

HTH

C.

安人多梦 2024-08-28 21:08:30

如果您需要更改文件中间的某些内容,则必须读取它,解析它并将其保存回来。否则,您只能在其末尾添加一些内容。

不过,您不应该担心此操作的成本,因为它是一个配置文件,不可能每秒更改一百次,所以它所花费的时间应该可以忽略不计。

如果您希望在访问/更新/删除方面具有更大的灵活性,我认为您应该考虑将配置文件移动到数据库表中。

If you need to change something in the middle of the file, you have to read it, parse it and save it back. Othwerwise you can only append something to the end of it.

You should not be concerned about the cost of this operation though, as it's a configuration file that won't likely be changed a hundred times every second, it should take negligible time.

If you want more flexibility in access/update/delete I think you should consider moving your configuration file to a database table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文