how hard

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

how hard

Post 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?
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post by gotDNS »

probably kinda hard :P :D
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

:-( i was hoping for an example of the shortest most basic way
lc
Forum Contributor
Posts: 188
Joined: Tue Apr 23, 2002 6:45 pm
Location: Netherlands

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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)
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

you totally lost me :( eh ill figure it out someday
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

please do so since I'm curious now to hear ;)
Anthron
Forum Newbie
Posts: 8
Joined: Mon May 20, 2002 6:25 pm
Location: usa
Contact:

Re: how hard

Post 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 :)
sandz
Forum Newbie
Posts: 3
Joined: Wed May 29, 2002 5:58 pm

Post 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)
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

to accomplish this you can read it with file(), unset($arr[$linenr]) join() it and write it back.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

what sandz wrote was exactly what i was trying to accomplish, thanks.

ill test it out in a minute
Post Reply