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>';
?>
May 12, 2012 11:52 AM
(This post was last modified: May 12, 2012 12:00 PM by alphabot.)
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.
(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
May 12, 2012 12:01 PM
(This post was last modified: May 12, 2012 12:20 PM by alphabot.)
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>';
?>
Just wondering, did mine work okay? Since I never actually tested it I don't know for sure!
May 13, 2012 11:26 AM
(This post was last modified: May 13, 2012 11:26 AM by YERANUK.)
$filepath = 'path/to/file'
file_put_contents($filepath, implode(array_slice(file($filepath), 0, 5)));
|