使用Fwrite编辑另一个PHP文件的PHP文件 [英] Editing a PHP File, with another php file, using Fwrite

查看:59
本文介绍了使用Fwrite编辑另一个PHP文件的PHP文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的最后一个问题没有很好地解释.

My last question wasn't explained very well.

我要在这里执行的操作是,使用另一个.php文件上的fwrite功能将数据插入到PHP文件中.

What I'm trying to do here is insert data into a PHP File, Using the fwrite feature on another .php file.

为简单起见,我将要插入的数据标记为file.php,将要使用fwrite执行的数据标记为edit.php

To keep this simple, I'm labelling the one I want data inserted as file.php and the one I'm using fwrite to execute on, is edit.php

现在,我把事情写下来了,我的问题是,需要在file.php上关闭php标记之前插入该数据.

Now, I got the writing thing down, what my problem is, is I need to INSERT that data, Before the closing php tag on file.php.

我想做的是删除关闭的php标记,写入数据,然后重写标记.

What I tried doing was, deleting the closing php tag, writing the data, and then rewriting the tag.

这是我的源代码:

<?php
$rows = file("file.php");    
$tagremove = "?>";
foreach($rows as $key => $row) {
if(preg_match("/($tagremove)/", $row)) {
    unset($rows[$key]);
}
}
file_put_contents("file.php", implode("", $rows));

$User = $_GET["user"];
$File = "file.php"; 
$Handle = fopen($File, "a");
fwrite($Handle, "");
fwrite($Handle, $User);
fwrite($Handle, "\r\n");
fwrite($Handle, "?>");
print "Data Written"; 
fclose($Handle); 
?>

当我在Edit.php上运行它时,它会将数据插入文件中,但仅写入第一行,并替换已经存在的任何内容. (在我的情况下,它是开头的php标签).我不知道自己在做什么错,或者是否有另一种方法可以做到,但是我们将不胜感激.

When I run this on Edit.php, it inserts that data into the file, but its only writing to the first line, and replacing whatever is already there. (In my case its the opening php tag). I don't know what I'm doing wrong, or if there is another way to do this, but any assistance would be appreciated.

这再次适用于聊天客户端.

this is again for a chat client.

我有一个文件,该文件将消息发送到客户端随后读取的.txt文件中.

I'm having a file, that sends a message into a .txt file that the client then reads.

该文件正在读取file.php(staff.php),以检查提交的用户是否是工作人员.

And that file is reading file.php (staff.php) to check if the user submitting is a staff member.

如果确实是用户是工作人员,那么它将更改send.php中的用户名变量.

If it comes up true that the user is a staff member, then it changes the username variable in the send.php.

到目前为止,send.php仅成功完成了工作,包括Staff.php,我尝试了staff.txt,原因是php代码在staff.php中.

And so far, the send.php has only sucessfully, included the Staff.php, I've tried staff.txt, and the reason is, php code is in the staff.php.

推荐答案

尝试一下:

$data="echo 'hello world!';";
$filecontent=file_get_contents('file.php');
// position of "?>"
$pos=strpos($filecontent, '?>');
$filecontent=substr($filecontent, 0, $pos)."\r\n".$data."\r\n".substr($filecontent, $pos);
file_put_contents("file.php", $filecontent);

请不要忘记,您需要检查用户的数据.

Please don't forget, that you need to check data from user.

这篇关于使用Fwrite编辑另一个PHP文件的PHP文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆