Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

how to make a php page that edits and clears another PHP file

Ok so I would need

/admin/logs.php
to view and be able to edit
../logs/log.php but after lets say the 5th line of code.
I would like to to clear it also and remove ALL text after line 5, But lines 1-5 should not be touched.

also i need
/admin/indexfile.php
to view and edit
../index.php
but the whole file.
Give this a go. I've not tested it at all so there a very good chance I've missed something. It's just the first one.

PHP Code

<?php

$separator 
= '// Edit below //';
$keep = array(
    
'Line 1',
    
'Line 2',
    
'Line 3',
    
'Line 4',
    
'Line 5',
    
$separator
);

if(isset(
$_POST['submit'])) {
    
$content = implode(PHP_EOL, $keep) . PHP_EOL . $_POST['content'];
    if(
file_put_contents($file, $content)) {
  echo 
'<h1>Done</h1>';
    } else {
  echo 
'<h1>Oops</h1>';
    }
}

$file = 'log.php';
$lines = file($file);
$current = array();
$result = array();

$editable = false;
for(
$x = 0; $x < count($lines); $x++) {
    if(
strpos(strtolower($lines[$x]), strtolower($separator)) != false) {
  
$editable = true;
    }
    if(
$editable) {
  
$current[] = $lines[$x];
    }
}

echo 
'<form action="" method="post">';
echo 
'<textarea name="content">';
foreach(
$current as $line) {
    echo 
"{$line}<br />";
}
echo 
'</textarea>';
echo 
'<input type="submit" name="submit"';
echo 
'</form>';

?>
Here is my version, you could put it into a function if you want.

Code
<?php
    $log = fopen("logs/log.php", "r");
    $t = array();
    for($i=0; $i<5; $i++) {
        $t[] =fgets($log);
    }
    fclose($log);

    $log = fopen("logs/log.php", "w");
    foreach($t as $v) {
        fwrite($log, $v);
    }

    //Write your stuff
    fwrite($log, "Test");

    fclose($log);
?>

benefit of my code is that it's dynamic.

EDIT: Meh just reread what you wanted. Anyway, you could incorporate mine into his to make his dynamic.
quit reading my shit
(May 12, 2012 11:52 AM)alphabot Wrote:  Here is my version, you could put it into a function if you want.

Code
<?php
    $log = fopen("logs/log.php", "r");
    $t = array();
    for($i=0; $i<5; $i++) {
        $t[] =fgets($log);
    }
    fclose($log);

    $log = fopen("logs/log.php", "w");
    foreach($t as $v) {
        fwrite($log, $v);
    }

    //Write your stuff
    fwrite($log, "Test");

    fclose($log);
?>

benefit of my code is that it's dynamic.

Okay man, did not work for one.
1) deleted my first 5 lines of code,
2) as soon as i run code it deletes everything i only want it to delete when i hit delete and display code on load under line 5, and only want to remove underline 5
Here we go, try this: (its based off of mine and his)

Code
<?php    
    $log = fopen("logs/log.php", "r");
    $t = "";
    for($i=0; $i<5; $i++) {
        $t .= fgets($log);
    }
    fclose($log);

    if(isset($_POST["submit"])) {
        $content = $_POST['content'];
        file_put_contents("logs/log.php", $t . $content);
    }

    echo '<form action="index.php" method="post">';
    echo '<textarea style="width:50%;height:50%" name="content">';
    echo file_get_contents("logs/log.php", NULL, NULL, strlen($t));
    echo '</textarea>';
    echo '<input type="submit" name="submit">';
    echo '</form>';

?>
quit reading my shit
Just wondering, did mine work okay? Since I never actually tested it I don't know for sure!
$filepath = 'path/to/file'
file_put_contents($filepath, implode(array_slice(file($filepath), 0, 5)));


Possibly Related Threads...
Thread / Author Replies Views Last Post
[PHP] by t0pemieFeb 20, 2018 12:04 PM 4 176 Feb 20, 2018 12:04 PM
Last Post: V3t3r4n
[PHP] by jamesgates1Jul 20, 2014 04:33 PM 8 303 Jul 20, 2014 04:33 PM
Last Post: jamesgates1
[PHP] by MilitiaSep 14, 2012 07:11 PM 0 62 Sep 14, 2012 07:11 PM
Last Post: Militia