Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8150521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:05:04+00:00 2026-06-06T15:05:04+00:00

My last question wasn’t explained very well. What I’m trying to do here is

  • 0

My last question wasn’t explained very well.

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

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

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.

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

Here is my source code for that:

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

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.

Edit: this is again for a chat client.

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

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

If it comes up true that the user is a staff member, then it changes the username variable in the send.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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:05:06+00:00Added an answer on June 6, 2026 at 3:05 pm

    Try this:

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to rephrase my question, cause my last one wasn't clear to everyone.
I posted this question last week, but I wasn't very clear on what was
Let me rephrase my last question, what PHP library or framework can I use
my last question was closed because it wasn't clear, so I'll try again because
Sorry if my last question is very similar, but I have another query of
My last question was somewhat narrower than this one. I am interested to know
My last question was about getting the string representation of an object serialized to
After having my last question answered , I have never see the preventDefault(); function
In relation to my last question ( Is there an easier way of passing
Following from my last question which @Jon Skeet gave me a lot of help

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.