Page 1 of 1
how hard
Posted: Tue May 28, 2002 4:37 pm
by hob_goblin
is it to make something that views the contents of a file and puts them into an array using file(), and lets me edit or delete a line individually?
Posted: Tue May 28, 2002 4:53 pm
by gotDNS
probably kinda hard

Posted: Tue May 28, 2002 6:20 pm
by hob_goblin

i was hoping for an example of the shortest most basic way
Posted: Tue May 28, 2002 7:35 pm
by lc
Not that hard.
basicly getting the lines isn't the problem. We can all get lines from an array and put them in sepparate edit fields/textareas and such. Or make something where you can click a line which then enters only that one in a form or something.
it's the write function that seems tricky but isn't. What I do after I had the form submit the altered line content is.
Code: Select all
$file = file("thefile");
\\ to get all the data
$fileї$nr] = "$sumbitted";
\\ where $nr is the line nr and $submitted is the new content
$a1 = fopen("thefile", "w+");
\\ to open the file and truncate it's length to zero
flock($a1,2);
\\ lock in case someone else tries to alter it
foreach ($file as $key){
$fw = fwrite($a1, $key);
}
\\ loop the fwrite to rewrite all data in the file including the altered line
fclose($a1);
\\ close file
It works
Posted: Tue May 28, 2002 7:54 pm
by volka
or doing it as a webform with textarea the very simple way
Code: Select all
<?php
<html><head>
<style type="text/css">
fieldset {width:600px; height:400px; }
textarea {width:100%; height:100%;}
</style>
</head><body>
<?php
if (isset($_POSTї'filecontent']) && isset($_POSTї'filename']))
{ // lots of checks
$fdWrite = fopen($_POSTї'filename'], "wb");
// hate \r\n
$_POSTї'filecontent'] = preg_replace("/\r\n|\r/", "\n", $_POSTї'filecontent']);
fwrite($fdWrite, $_POSTї'filecontent']);
fclose($fdWrite);
$filename = $_POSTї'filename'];
}
else
$filename = 'whatever.txt'; // where ever you get it from
?>
// output of the file-content, wether it was altered or not
<form method="POST">
<fieldset>
<legend><?php print($filename); ?></legend>
<input type="hidden" name="filename" value="<?php print($filename)?>"/>
<?php //doing this in php to avoid extra whitespaces
print(<textarea name="filecontent">);
readfile($filename);
print('</textarea>');
?>
</fieldset>
<input type="submit"/>
</form>'
</body></html>
at least I hope so (crashed my server once again -> reinstall grrrrr)
Posted: Tue May 28, 2002 10:30 pm
by hob_goblin
you totally lost me

eh ill figure it out someday
Posted: Wed May 29, 2002 8:34 am
by volka
please do so since I'm curious now to hear

Re: how hard
Posted: Wed May 29, 2002 5:09 pm
by Anthron
hob_goblin wrote:is it to make something that views the contents of a file and puts them into an array using file(), and lets me edit or delete a line individually?
what exactly are you trying to do. I'm very good with the flat-file stuff

Posted: Wed May 29, 2002 5:58 pm
by sandz
[syntax=php]<?php
function delete_line($file_name,$line_number)
{
// open connection to file
$F = fopen($file_name, rb");
while(!eof($F))
{
// put each line into an array, line 1 = $array[0], line 2 = $array[1] etc
$array[] = fgets($F,1024);
}
// close connection to file
fclose($F);
// array starts from 0 your, first line in your file starts from 1, so decrease $line_number by one
--$line_number
// delete line from array
$array[$line_number] = null;
// put array into string ready for writing back to file, with line deleted
$str = '';
for($i=0;$i < sizeof($array);$i++)
{
// if array element is set
if($array[$i] <> null)
{
// add this line back to the file
$str.= $array[$i]."\n";
}
}
// open file and remove contents
$F = fopen($file_name,"w+b");
// write string to file
fwrite($F,$str);
// close connection to file
fclose($F);
return true;
}
?>[/syntax]
well you get the idea hopefully, (not tested this btw might miss out the last line of your file just make the for loop go on 1 turn longer if it does)
?>
Posted: Wed May 29, 2002 6:28 pm
by volka
to accomplish this you can read it with file(), unset($arr[$linenr]) join() it and write it back.
Posted: Wed May 29, 2002 7:23 pm
by hob_goblin
what sandz wrote was exactly what i was trying to accomplish, thanks.
ill test it out in a minute