how hard
Moderator: General Moderators
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
how hard
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?
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
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.
It works
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 fileor doing it as a webform with textarea the very simple way
at least I hope so (crashed my server once again -> reinstall grrrrr)
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>- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
Re: how hard
what exactly are you trying to do. I'm very good with the flat-file stuffhob_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?
[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)
?>
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)
?>
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact: